我正在尝试为两个类Circle和Square实现一个工厂,它们都继承自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";
我想创建一个以形状类型(字符串)为键,以相应派生类的getInstance()函数指针为值的映射表。这可行吗?
谢谢, Kiran
static
和virtual
。你的类到底长什么样子? - James McNellis