Reputation: 4975
I am sorry for the long text but I would like to present the problem I am trying to solve with this as well as the actual problem. Maybe I will be pointed to another, possibly better approach.
I would like to create an eventtriggerred handler system. I will have several handler modules. The basic idea is that during initialization these modules will register for events they handle (register(module,eventItGetsCalledFor)) in a eventlist. In the case an event is triggered, it is looked up in the eventlist and all modules listed there are created and run. To achieve this all the modules have an interface.
My two questions are:
Since I only want to instantiate the modules upon the actual event I need either a way to store a pointer to the constructor ("virtual constructor"), since there are no virtual constructors as far as I know I go with a Factory method which I would like to add to the interface. The classic approach I know of would be:
class myInterface {
public:
...
static virtual *myInterface builder() = 0; //is static (pure) virtual possible?
};
class example : public myInterface {
public:
...
static virtual *myInterface builder() { return new example(); }
};
I would then have to store pointers to the builder() functions in the event list and execute the functions in case the event occurs to create the objects. I am wondering if there is a way arround writing static virtual *myInterface builder() { return new example(); }
for every example module.
A template approach would be to put it like this:
template static virtual *myInterface builder() { return new T(); }
However I dont like it to much since I would have to write the following to register a module for an event:
register(eventName,builder())
considering that I would like to create only one static register function in each module like so:
class example: public myInterface {
static void registerMe {
register(event1,builder<example>());
register(event2,builder<example>());
...
register(eventn,builder<example>());
}
};
it would be way more convenient if a call like register(event)
would be sufficient.
i could add register(event)
to the interface, but I dont want to rewrite the register function for every template which would read
void register(event) { getListForEvent(event).pushBack(builder<example>()); }
(the builder-template instantiation parameter varies), and making it a template itself is not to good either, since this brings me back to writing register<myclass>()
over and over.
So is there a more conviniant way? (During the pondering and writing of this I came up with inheriting from a template class. If polimorphism works with template classes with different instantiation parameters. Otherwise I have to use an Interface class, wrap it in a template and Inherit from that )
The second question is I have different classes derived from my interface class. However they have members which are not defined in the Interface. I would like to store a functionpointer to a member function, how do I do this? The type has to accept different base classes, but the rest of the function signature is the same (see example below)
class A : public interface {
void myFunc()
};
class A : public interface {
void yourFunc()
};
whatType* a = &A::myFunc;
whatType* b = &B::yourFunc;
Upvotes: 0
Views: 1270
Reputation: 188
Static member functions do not take this pointer as the first parameter while non-static member functions do, so a static member function can be considered as a global function within the specific namespace. That is why you cannot write like this:
static virtual *myInterface builder() = 0;
There is no way to define a function type like that. If there are functions in different classes but with same signature, consider to have a new interface inherited your first interface and add virtual members to the new interface. When you get a base interface pointer, use dynamic_cast to get the original type pointer from the base interface pointer, and then call the new member function. Alternately you can try Boost::Bind. It seems helpful in your situation.
Upvotes: 1
Reputation: 393124
//is static (pure) virtual possible?
No
CRTP (Curiously recurring template pattern) to the rescue
template <class Derived>
class myInterface {
public:
...
virtual *myInterface builder() = 0;
Derived* buildDerived()
{
return dynamic_cast<Derived*>(builder());
}
};
Upvotes: 3