我是 C++ 中的这个工厂模式的新手,我在尝试实现其中一种方法时得到了 header ,但我收到以下错误:
在静态成员函数中无效使用成员'creationFunctions'
typedef Shape (*createShapeFunction)(void);
/* thrown when a shape cannot be read from a stream */
class WrongFormatException { };
class ShapeFactory {
public:
static void registerFunction(const std::string &string, const createShapeFunction *shapeFunction);
static Shape *createShape(const std::string &string);
static Shape *createShape(std::istream &ins);
private:
std::map<std::string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory *getShapeFactory();
};
void ShapeFactory::registerFunction(const std::string &string, const createShapeFunction *shapeFunction)
{
creationFunctions.at(string) = shapeFunction;
}
最佳答案
您不能从静态成员函数访问类的非静态成员。在您的示例中,最好的想法可能是使所有成员函数都成为非静态的,除了“getShapeFactory”,然后它将充当 Mayer 的单例生成器...
关于c++ - 工厂模式 - 在静态成员函数中无效使用成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15723058/