• Advertisement

Archived

This topic is now archived and is closed to further replies.

is this ugly coding?

This topic is 5038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Your code is illformed.

void* cannot be implictly converted to type DummyforDynamicCast*.

Share this post


Link to post
Share on other sites
well, I know it can't be "successfully" converted to DummyforDynamicCast*, but with C-style casting you can do anything... (note: this code DOES work correctly, as far as I have tested it...)

after casting the void* to DummyforDynamicCast* the new "interface" WILL never be used, except for in the dynamic_cast.

now since DummyforDynamicCast* has at leas one virtual member, it makes it possible to use dynamic_cast on it. And then the dynamic_cast will check the virtual function table of the object DummyforDynamicCast* points to... So a valid system will be checked if it is possible to cast to the "U* pInterface" in the void QuerySystem(T systemID, U* pInterface).

How would you guys make something like this?



[edited by - bilsa on May 9, 2004 10:33:56 AM]

Share this post


Link to post
Share on other sites
It would be much better to use a integer(or possible an enum or something else suitable), maybe as a typedef, as id, and a common baseclass for the interface. Then no templates would be needed.

You would have

class ISystems
{
public:
virtual void QueryActiveSystem(int factoryID, BaseInterface* pInterface);
virtual void QuerySystem(int systemID, BaseInterface* pInterface);
};


This would not affect the functionality in any way, as if you are using templates, all functions still need to be defined in the interface you pass in, so why don''t have a base interface with all those function defined, either pure virtual, or just virtual.

Share this post


Link to post
Share on other sites

  • Advertisement