Because of RAII features i want my objects to be placeable only on the stack and also as object creation should be delegated to specialised factories i dont want ocpy constructor to be accessible for use.
So i did something like this.
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
}
I can do something like this
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();
}
But it doesn't seems to look right.
What are other ways to deal with that?