ParaToxic Posted October 20, 2012 Share Posted October 20, 2012 Hei there I have here a little problem with the design of a componentmanager. My idea is it to make a componentmanager, which loads components and add them to a map.Then the component can be login to the mainthread or to a extern thread, which will be created. Now the problem is, that I use Polymorphism to make a baseclass ( here IComponent ) and inherit this from my first component , the IConsole. When I write ISmartPointer<IComponent> _console(new IConsole); I can add that to the map, because I have always this IComponent as baseclass.So I add it to a map,lets say std::map<std::string,IComponent*> _map; Now I have virtual Functions in the IComponent class like Update,Initialize and Terminate. This virtual functions are defined in the component itself. But now the problem is that I wan't to call functions from the IConsole itself.There is for example a function LoginVar(...), but I can't write _console.get()->LoginVar(), because I have IComponent as the baseclass and this class haven't got a virtual function LoginVar..... This is a big problem.I want that I can add the Components to a map ( I think that the best way is to make it with Polymorphim), but also call the specific functions... I hope you can help me Thanks Quote Link to comment Share on other sites More sharing options...
Rick Posted October 20, 2012 Share Posted October 20, 2012 You'd have to cast it to your specific component. Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted October 20, 2012 Author Share Posted October 20, 2012 Look at that: #ifndef ICOMPONENTMANAGER_H_ #define ICOMPONENTMANAGER_H_ #pragma once #include "engine.h" #include "GlobalDefine.h" #include "IComponent.h" #include "IConsole.h" #include <stdlib.h> #include <map> class IComponentManager { public: IComponentManager(void); ~IComponentManager(void); IResult Initialize(HINSTANCE hinst); IResult Terminate(); void Update(); //--------------Console---------------// template<typename T> inline void Console_LoginVar(std::string const& name, T& ref) { _debugConsole.get()->LoginVar(name,ref); } // That doesn't work because the baseclass hasn't got a function LoginVar, only the IConsole class //------------------------------------// private: ISmartPointer<IComponent> _debugConsole; std::map<std::string,IComponent*> _mainThread; std::map<std::string,IComponent*>::iterator _mainItr; std::map<std::string,std::pair<IComponent*,HANDLE>> _externThread; void LoginMainThread(std::string s,IComponent* comp); void LoginExternThread(std::string s,IComponent* comp); }; #endif Quote Link to comment Share on other sites More sharing options...
Ruki Posted October 20, 2012 Share Posted October 20, 2012 _debugConsole.get()->LoginVar(name,ref) cast _debugConsole.get() to iConsole ( (iConsole*)_debugConsole.get() )->LoginVar( name, ref ) or ( (iConsole*)(_debugConsole.get()) )->LoginVar( name, ref ) perhaps? Quote Link to comment Share on other sites More sharing options...
Ruki Posted October 20, 2012 Share Posted October 20, 2012 If that works, you might want to make a function that will cast it for you, such as iConsole* get_iConsole( BaseClass baseclassIn ) { // Check for type if ( baseClassIn->type == TYPE_iCONSOLE ) // return Return (iConsole *)baseClassIn; else // Return return NULL; } which will simplify the casting for you and check the inherited item is the right type before trying to call ->LoginVar on it? I wrote a whole gui system based on inheritance and it turned out to be more hassle than it's worth because of all this casting stuff, good luck! Quote Link to comment Share on other sites More sharing options...
ChrisMAN Posted October 20, 2012 Share Posted October 20, 2012 :trollface: Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted October 20, 2012 Author Share Posted October 20, 2012 Are you kidding me ???? Thanks so much , have to look to the old whise C++ book..... With static_cast you can take the functions of the baseclass directly : static_cast<IConsole*>(_debugConsole.get())->Test(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.