工厂方法 C++

Factory Method C++

我是 C++ 的初学者,我试图解决工厂方法的这个实现,我试图用函数 make object 寻找实现下面是:

  1. 检索 return 类型以提供对 在下面的class对象中创建的对象。
  2. 为给定的 id[=38 创建两个对象(Object2Object3) =]
  3. 尝试线程安全。

我正在编写以下代码:

enum ObjectType {
    FO_Object2, FO_Object3
};

class Object {
    int ID;
public:
    Object(int id) : ID(id) {}
    Object(const Object&) = delete;

    virtual void printObject() = 0;
    static Object* make_object(int id);
};

class Object2: public Object
{
public:
    void printObject() {
        std::cout << "ID = 2" << std::endl;
    }
};

class Object3 : public Object
{
public:
    void printObject() {
        std::cout << "ID = 3" << std::endl;
    }
};

//Factory Method to create objects
Object* Object::make_object(int id)
{
    switch (id)
    {
        case FO_Object2: return new Object2();
        case FO_Object3: return new Object3();
        default:
            return NULL;
    }
}

class object_factory
{
public:
    object_factory()
    {
        ObjectType id = FO_Object3;
        pObject = Object::make_object(id);
    }
    ~object_factory()
    {
        if (pObject) {
            delete[] pObject;
            pObject = NULL;
        }
    }
    Object* getFactory() {
        return pObject;
    }
private:
    Object* pObject;
};

int main()
{
    object_factory* pFactory = new object_factory();
    Object* pObject = pFactory->getFactory();
    pObject->printObject();

    return 0;
}

但不幸的是,在我的错误列表中提示我有以下错误:

E1790 --> 无法引用 "Object2" 的默认构造函数 -- 它是一个已删除的函数。

E1790 --> 无法引用 "Object3" 的默认构造函数 -- 它是已删除的函数。

C2280 --> 'Object2::Object2(void)': 试图引用已删除的函数

任何人都可以帮助我解决这个问题,在此先感谢。

这是一个更正后的示例(据我了解您想要实现的目标)。请参阅代码注释以阐明更改内容的原因。需要做更多的工作才能使核心更健壮(例如,在基础中使用 ObjectType class)

#include <iostream>

enum ObjectType {
    FO_Object2 = 2,
   FO_Object3 = 3
};

class Object {
public:
    int ID; // ID must be public so that inherited class can access it. Otherwise create getter for ID
    Object(int id) : ID(id) {};
    Object(const Object&) = delete;

    virtual void printObject() = 0;
    static Object* make_object(int id);
};

class Object2: public Object
{
public:
    Object2():Object(2) {}; // specifically instanciate a base class whose id is 2
    void printObject() {
        std::cout << "ID = "<< ID << std::endl;
    }
};

class Object3 : public Object
{

public:
    Object3():Object(3) {}; // specifically instanciate a base class whose id is 3
    void printObject() {
        std::cout << "ID = "<< ID << std::endl;
    }
};

//Factory Method to create objects
Object* Object::make_object(int id)
{
    switch (id)
    {
        case FO_Object2: return new Object2();
        case FO_Object3: return new Object3();
        default:
            return NULL;
    }
}

class object_factory
{
public:
    object_factory()
    {
        ObjectType id = FO_Object3;
        pObject = Object::make_object(id);
    }
    ~object_factory()
    {
        if (pObject) {
            delete[] pObject;
            pObject = NULL;
        }
    }
    Object* getFactory() {
        return pObject;
    }
private:
    Object* pObject;
};

int main()
{
    object_factory* pFactory = new object_factory();
    Object* pObject = pFactory->getFactory();
    pObject->printObject();

    return 0;
}