Storing member function pointers of derived classes in map

Posted by Kiran Mohan on Stack Overflow See other posts from Stack Overflow or by Kiran Mohan
Published on 2010-12-26T10:22:36Z Indexed on 2010/12/26 10:53 UTC
Read the original article Hit count: 315

Filed under:
|

Hello,

I am trying to implement a factory for two classes Circle, Square both of which inherits from Shape.

class Shape {
public: 
    virtual static
    Shape * getInstance() = 0;

};

class Circle : public Shape {        
public:
    static const std::string type;

    Shape * getInstance() {
        return new Circle;
    }
};
const std::string Circle::type = "Circle";

class Square : public Shape {        
public:
    static const std::string type;

    Shape * getInstance() {
        return new Square;
    }
};
const std::string Square::type = "Square"; 

I want to now create a map with key as shape type (string) and value as a function pointer to getInstance() of the corresponding derived class. Is it possible?

Thanks, Kiran

© Stack Overflow or respective owner

Related posts about c++

Related posts about function-pointers