3

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.

rlbond
  • 65,341
  • 56
  • 178
  • 228
coanor
  • 3,746
  • 4
  • 50
  • 67
  • @coanor: welcome to stack overflow. Take a few minutes to get acquainted with the formatting options of the editor - for now I've re-formatted your post to show code properly – Eli Bendersky Jun 05 '10 at 05:00
  • Hm, why really is that `Unique_handle` uncopiable? I.e. why not have a (copiable) handle-to-unique?? – mlvljr Jun 05 '10 at 06:36
  • 2
    @coanor: what are you trying to achieve with it? What is the point in creating a unique handle and then trying to copy it so it is no longer unique? What are you trying to do? – jalf Jun 05 '10 at 12:19

6 Answers6

6

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
  • 263,248
  • 89
  • 350
  • 412
  • 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 Jun 05 '10 at 08:37
  • @coanor: read about implementing the singleton pattern in C++. There are many approaches - pick one that's suitable for your needs – Eli Bendersky Jun 05 '10 at 08:45
  • 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 Jun 05 '10 at 12:20
  • @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 Jun 05 '10 at 13:21
  • 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 Jun 05 '10 at 13:32
4

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
  • 333,147
  • 50
  • 533
  • 760
2

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
  • 45,396
  • 28
  • 101
  • 140
2

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
  • 43,549
  • 15
  • 93
  • 156
  • 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 Jun 05 '10 at 11:15
0

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
  • 3,746
  • 4
  • 50
  • 67
0

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.
  • 287,565
  • 48
  • 449
  • 722