c++ - 工厂模式 - 在静态成员函数中无效使用成员

标签 c++ factory-pattern

我是 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/

相关文章:

c++ - 使用use_future的Boost Asio async_send_to多播不起作用

c++ - 调整原子 vector 的大小?

c++ - boost::序列化和工厂模式设计

c# - 满足开放/封闭原则的工厂模式?

java - 为什么框架要实现工厂方法来创建简单的原始对象?

java - 我的工厂类有什么问题吗?

c++ - 通过近似选择颜色区域

c++ - 当我聚合初始化数组时,Clang 会发出警告,而 gcc 不会

c++ - 使用 C++ 将 RGBA 转换为 YUV422 格式 - 没有得到正确的输出

使用通用约束的 Java 工厂模式