Now today when I made an interface for my Systems class, I wanted to have a templated virtual function... but soon got the fact that I cant...
the functions I wanted to be virtual was:
class ISystems {
public:
template <class T, class U>
virtual void QueryActiveSystem(T factoryID, U* pInterface) {
//not possible to have a virtual member template
}
template <class T, class U>
virtual void QuerySystem(T systemID, U* pInterface) {
//not possible to have a virtual member template
}
//...
}
But that is not possible... So I thought... define the functions non virtual and let them call for a virtual function that does what we want...
So I did like this:
class ISystems {
public:
virtual void LoadModule(const char* sModuleName) = 0;
template <class T, class U>
void QueryActiveSystem(T factoryID, U* pInterface) {
DummyForDynamicCast* tmpInterface = getActiveSystem(factoryID);
*pInterface = dynamic_cast<U>(tmpInterface);
}
template <class T, class U>
void QuerySystem(T systemID, U* pInterface) {
DummyForDynamicCast* tmpInterface = getSystem(systemID);
*pInterface = dynamic_cast<U>(tmpInterface);
}
private:
class DummyForDynamicCast {
virtual void dummy() = 0;
};
virtual void* getSystem(STORM_GUID id) =0;
virtual void* getActiveSystem(STORM_GUID id) =0;
};
I had to make a "helping" virtual function that returns a void* - pointer to the system I want to retrieve... :
virtual void* getSystem(...) =0;
virtual void* getActiveSystem(...) =0;
But dynamic_cast cant cast a void* so I made a DummyClass with one virtual function and casted the void* pointer to the DummyClass and the dynamic_cast works just as I wanted...
Now this doesn't feel like the right way of doing this :/
So would you guys say this is Ugly/poor coding?
[edited by - bilsa on May 9, 2004 9:30:38 AM]
[edited by - bilsa on May 9, 2004 9:34:05 AM]