In reading TCPL, I got a problem, as the title refered, and then ‘private’ class is:
class Unique_handle {
private:
Unique_handle& operator=(const Unique_handle &rhs);
Unique_handle(const Unique_handle &rhs);
public:
//...
};
the using code is:
struct Y {
//...
Unique_handle obj;
};
and I want to execute such operations:
int main()
{
Y y1;
Y y2 = y1;
}
although, these code are come from TCPL, but I still can not got the solution…
Can anybody help me, appreciate.
As its name suggests, the
Unique_handleisn’t meant to be copied. Its implementation ensures it by disabling the copy constructor and copy assignment operator.One solution for multiple instances having access to a
Unique_handleis holding a pointer to it, and copying the pointer. Then multiple instances ofYpoint to the same unique handle.Take care, however, to manage resources properly in this case.