The first idea is that to get the polymorphism you need to call the member method by pointer or link. I would save the pointer to the base class on the map (the stored item will be copied), and then call it using the pointer as follows:
#include <iostream> #include <map> class Base { public: virtual void Foo() { std::cout << "1"; } }; class Child : public Base { public: void Foo() { std::cout << "2"; } }; int main (int argc, char * const argv[]) { std::map<std::string, Base*> Storage; Storage["rawr"] = new Child(); Storage["rawr"]->Foo(); return 0; }
You get the result "2".
Note. Now you have to take care of the allocated memory.
source share