[LC++]Copy constructor.. private?????? :?

Jack Lloyd lloyd at acm.jhu.edu
Mon Aug 20 16:28:04 UTC 2001


> Well I can understand a protected FXLabel() constructor.. but what
> surprises me it is the private copy construtor and the operator=()
>
> I always have made this public.. am I missing something?...
>
> Can somebody explain me this please?

Generally this is done when, for whatever reason, copying this object would
be a bad idea. For example, if I wrote a class that wrapped around a simple
Unix file descriptor:

class MyFile
{
   public:
     // read, write, whatever
   private:
     int file;
};

I wouldn't want you to be able to copy it, because there is no reasonable
way to "copy" a file descriptor. I could, for example, copy the file it was
attached to, or something like that, but that's obviously a very bad idea.
Nor I can simply leave them as references (of sorts) to each other [by
simply copying the integer value of file from one object to another],
because calling close() on one would affect the other, and so on.

So basically the fact that operator= and the copy contstructor are private
means that whoever wrote the code doesn't think it's a good idea for you to
copy this object.

Regards,

Jack




More information about the tuxCPProgramming mailing list