I have not used C++ in a long time. I'm trying to display some polymorphic behavior:
class func {
public:
virtual void print() = 0;
};
class func1 : public func {
public:
void print () { cout << "FUNC 1" << endl; };
};
class func2 : public func {
public:
void print () { cout << "FUNC 2" << endl; };
};
static map<string,func *> myMap;
static func1 f1 = func1 ();
static func2 f2 = func2 ();
myMap["func1"] = &f1;
myMap["func2"] = &f2;
So In my main function, when I call:
myMap["func1"]->print();
myMap["func2"]->print();
I would expect:
FUNC 1
FUNC 2
Not sure if this is the right way to do this. When I compile the code, it gives me this error:
test.cc:31: error: expected constructor, destructor, or type conversion before ‘=’ token
test.cc:32: error: expected constructor, destructor, or type conversion before ‘=’ token
Which refers to these lines:
myMap["func1"] = &f1;
myMap["func2"] = &f2;
Thank you.