I would like to echo anon and ask why do you want to do this in the first place.
I would like to echo anon and ask why do you want to do this in the first place.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'll echo the same query and reiterate my question on the safety of casting a child method pointer to its parent. Is this safe? Seems to be breaking a lot of things I wouldn't have thought possible, to be honest.
The Purpouse is simply Play a puzzle game with C++
Well this is the architecture of my wired application
--------------------
There is a Map like.
buy => theBuyMethod()
sell => theSellMethod()
accept a LISTENERNAME as string.
Now you have to call the method designated to the LISTENER
e.g. If the user enters buy call theBuyMethod.
I cant use a switch case here cause I want it to bind automatically.
If I use a switch case I need to update the switch case each time I add a new member.
am I able to make you understand.
If you already have a map, why not just find the string in the map's keys? If it is found, call the function that the key maps to.
You would need to update the map each time you add a new function, but this is the point of the map.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This is what currently going on.
But its showing problems (which is solved using reinterpret_cast) during polymorphic Classes.
Ah yes, I see that now. It did sound very much like what twomers proposed.
At the moment, one problem is that you have to map to the actual member functions themselves, so polymorphism is problematic. One possible solution that comes to mind is to map to a free function that takes an object by (const) reference and then calls the appropriate member function. Polymorphism will then work as intended. For example:
Code:#include <iostream> #include <map> #include <string> class Base { public: virtual void foo() const { std::cout << "Base::foo()" << std::endl; } }; class Derived : public Base { public: virtual void foo() const { std::cout << "Derived::foo()" << std::endl; } }; void foo(const Base& x) { x.foo(); } typedef void (*MappedFunc)(const Base&); int main() { std::map<std::string,MappedFunc> functions; functions["foo"] = foo; Derived derived; std::map<std::string,MappedFunc>::iterator iter = functions.find("foo"); if (iter != functions.end()) { iter->second(derived); } else { std::cout << "No such function." << std::endl; } }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)