C++ C++;使用受保护构造函数和复制构造函数创建非堆工厂对象

C++ C++;使用受保护构造函数和复制构造函数创建非堆工厂对象,c++,copy-constructor,factory-pattern,protected,deleted-functions,C++,Copy Constructor,Factory Pattern,Protected,Deleted Functions,由于RAII的特性,我希望我的对象只能放在堆栈上,而且由于对象创建应该委托给专门的工厂,我不希望ocpy构造函数可以访问使用 所以我做了类似的事情 template<typename Product, Args ... > class Creator : public Product { public: static Product create(Args ... args) { return Product(args

由于RAII的特性,我希望我的对象只能放在堆栈上,而且由于对象创建应该委托给专门的工厂,我不希望ocpy构造函数可以访问使用

所以我做了类似的事情

template<typename Product, Args ... >
class Creator : public Product
{
    public:
        static Product create(Args ... args)
        {
            return Product(args ... );
        }
};

class ProtectedClass
{
        ProtectedClass(const ProtectedClass& aThat)=delete;
        ProtectedClass& operator=(const ProtectedClass& aThat)=delete;
    protected:
        ProtectedClass(){}
};

class Spawner
{
    public:
        ProtectedClass getProtectedClass()
        {
            return Creator<ProtectedClass>::create();
        }
}

int main()
{
    Spawner spawner;
    //I need protectedClass to be enclosed within this frame
    ProtectedClass protectedClass = spawner.getProtectedClass(); // err copy constructor is delted
}
模板
类创建者:公共产品
{
公众:
静态产品创建(Args…Args)
{
退货产品(args…);
}
};
类保护类
{
ProtectedClass(const ProtectedClass&aThat)=删除;
ProtectedClass&operator=(const ProtectedClass&aThat)=删除;
受保护的:
ProtectedClass(){}
};
类产卵器
{
公众:
ProtectedClass getProtectedClass()
{
返回创建者::create();
}
}
int main()
{
产卵器产卵器;
//我需要将protectedClass封装在此框架内
ProtectedClass ProtectedClass=spawner.getProtectedClass();//删除了错误复制构造函数
}
我可以这样做

template<typename Product, Args ... >
class Creator : public Product
{
    public:
        Creator(Args ... args) : product_(args ...){}
        Product& get() const
        {
            return product_;
        }
    private:
        Product product_;
};

class Spawner
{
    public:
        std::unique_ptr<Creator<ProtectedClass>> getProtectedClassCreator()
        {
            return new Creator<ProtectedClass>();
        }
}

int main()
{
    Spawner spawner;
    std::unique_ptr<Creator<ProtectedClass>> creator = std::move(spawner.getProtectedClassCreator());
    ProtectedClass& protectedClass = creator->get();
}
模板
类创建者:公共产品
{
公众:
创建者(Args…Args):产品(Args…{}
Product&get()常量
{
退货产品;
}
私人:
产品;;
};
类产卵器
{
公众:
std::unique_ptr getProtectedClassCreator()
{
返回新的创建者();
}
}
int main()
{
产卵器产卵器;
std::unique_ptr creator=std::move(spawner.getProtectedClassCreator());
ProtectedClass&ProtectedClass=creator->get();
}
但看起来不太对劲


还有什么其他方法可以解决这个问题呢?

我的方法是删除副本,启用移动,并允许通过任何可以创建构造键的类进行构造

// forward declare any factories
class Spawner;

struct ProtectedClass
{
    class PermissionKey {
        // this is a private constructor
        PermissionKey() {};

        // make friends of the factories
        friend Spawner;
    };

    // all this is now public.
    // because we have declared a constructor, the default constructor
    // is deleted.    
    ProtectedClass(PermissionKey) {}

    // disable copies
    ProtectedClass(const ProtectedClass& aThat)=delete;
    ProtectedClass& operator=(const ProtectedClass& aThat)=delete;

    // enable moves so the factory can return it
    ProtectedClass(ProtectedClass&& aThat)=default;
    ProtectedClass& operator=(ProtectedClass&& aThat)=default;
};

class Spawner
{
public:
    ProtectedClass getProtectedClass()
    {
        // construct our spawned object - we can create keys
        return ProtectedClass(ProtectedClass::PermissionKey());
    }
};

int main()
{
    Spawner spawner;
    //I need protectedClass to be enclosed within this frame
    auto protectedClass = spawner.getProtectedClass(); // ok now
}

最后我选择了独特的ptr。每次移动操作都涉及到DTOR时,会出现很多问题。@user1079475您比我更了解这个用例。但是我认为您想要堆栈上的对象?是的,我想要,但是将对象从方法范围移动到调用范围太麻烦了。这些对象具有互斥体属性,因此需要在堆上移动这些互斥体,以此类推。麻烦多,收获少