views:

119

answers:

6

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.

+5  A: 

As its name suggests, the Unique_handle isn'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_handle is holding a pointer to it, and copying the pointer. Then multiple instances of Y point to the same unique handle.

Take care, however, to manage resources properly in this case.

Eli Bendersky
Thank you very much, I googled my question, and find a way to construct such a object: static Unique_handle* instance() { return new Unique_handle(); } but it seems wrong, then how can I define such a object outside?
coanor
@coanor: read about implementing the singleton pattern in C++. There are many approaches - pick one that's suitable for your needs
Eli Bendersky
Oh god, so that's where we are now? If you define a private copy constructor, and regret it, the best answer is to make it a *singleton*? I weep for the code to be written in the coming years.
jalf
@jalf: while taken out of context, your plea makes sense, I'm not sure it does in this case. The object the OP presented appears one that really should be a singleton.
Eli Bendersky
@Eli: I don't see why. I don't think there's enough information in the question to in any way tie it to a singleton. He doesn't say that he want to guarantee that exactly one instance of the class exists, nor that it should be globally accessible
jalf
+1  A: 

Usually, the idiom of making your copy constructor and assignment operator private (and unimplemented) implies that the original author of the class specifically did not want this object to be copyable.

dicroce
A: 

you shouldn't try to copy it. But having said that.... you can memcpy it. Only reason you'd do that is that you know what your doing, you know the consequences, etc etc. Its generally stepping out of the bounds of whats generally acceptable. But you might want to do something a bit ninja.

Keith Nicholas
Worst. Advice. Ever.
Matthieu M.
as I said, its not recommended, but if you do want to copy it, then you can.... maybe you want to hot swap in some proxy for some weird reason.
Keith Nicholas
+3  A: 

The example you're looking at in Stroustrup's book is demonstrating how the designer of a class can explicitly prevent copying or assignment of objects of that class.

The class is intentionally making it so your code can't do what you're trying to do (presumably because the class won't function properly or copying otherwise doesn't make sense). If you want to be able to copy objects of that class, you'll need to redesign the class.

Some other options you might have (which also might not make sense, but that depends on what your really doing) - pass around pointers to references to the object instead of copying.

Michael Burr
A: 

I have googled my question, and find a way to construct such a object:

static Unique_handle* instance() { return new Unique_handle(); }

but it seems wrong, then how can I define such a object outside?

Anyway, thank you for all of your concern.

coanor
A: 

You can use a shared_ptr to share the object:

class Y
{
public:
  Y(): mHandle(new UniqueHandle()) {}

private:
  boost::shared_ptr<UniqueHandle> mHandle;
};

It's as simple as that.

If you don't want shared ownership, you can use boost::scoped_ptr or the newly created std::unique_ptr if you have access to it, and then implement the CopyConstructor and AssignmentOperator yourself, taking care of their semantics.

Matthieu M.