3 ms·
In 1998 C++, we can solve the problem of how to make a class non-copyable, even to code that is in its class scope. We can create a dummy class which has a pri
by kazinator 10y ago
In 1998 C++, we can solve the problem of how to make a class non-copyable, even to code that is in its class scope.
We can create a dummy class which has a private copy constructor that is not implemented:
class noncopyable {
private:
noncopyable(const noncopyable &);
};
then simply make this a data member of a class that you don't wish to be copyable:
class whatever {
private:
noncopyable nc;
};
The default copy constructor generated by the C++ compiler performs a member-for-member copy, which involves copying nc. That is not possible, so in effect that copy constructor is defeated, not unlike by "= delete".
Alternatively use inheritance to mix this in as a trait:
class whatever : private noncopyable {
// ...
};