2 Questions: Do I need an interface & C++ -> D code part, how?
Benjamin Schulte
Aldoric at gmx.de
Sat May 19 02:54:38 PDT 2007
Hi,
I've got two questions about D - all containing those class structure:
class Event
{
this( ) { ... } /// Add event to eventlist
~this( ) { ... } /// Remove event from eventlist
abstract Event[] getEventList( );
}
class MainLoopEvent : Event
{
abstract void onMainLoop( );
Event[] getEventList( ) { return events; }
MainLoopEvent[] events;
}
-----
now my first question:
I've got another class, called MyApplication - I want to have class Application as base class and MainLoopEvent as 2nd base class. So I wrote:
class MyApplication : Application, MainLoopEvent { }
But I got an error, that MainLoopEvent has to be an interface. Is there now another way than saying:
interface EventInterface
{
// EMPTY!!!
}
class Event : EventInterface
...
Would be nice if I don't have to create an empty interface, just to make D happy.
---------------
2nd question:
MainLoopEvent has the method onMainLoop( );
Say we create a second class with Event as Base class
class AnotherEvent : Event
{
abstract void onBeingHappy( int a, int b );
}
Now, now I need a new function. In C++ I could write a macro: (I mixed C++ with D to show you what I mean:)
#define callEvent(classType,event) foreach(Event e; event.getEventList() ) ((classType*)e)
I could now just call
callEvent(MainLoopEvent, myApplication)->onMainLoop( );
callEvent(AnotherEvent, myEvent)->onBeingHappy( 12, 31 );
At the moment my solution for MainLoopEvent looks like this:
static void callMainLoop( )
{
// Call main loop event
foreach( MainLoopEvent e; events ) e.onMainLoop( );
}
But that's not my favorite way to do this, because I would have to rewrite this for every abstract method.
Might have some bugs in here, but I hope you understand what I mean. A template that calls methods I don't really know from the structure.
Thanks for every help : )
More information about the Digitalmars-d
mailing list