copy constructors and the assignment operator

J

John Ratliff

What are the ramifications of creating a private copy constructor?

Say I didn't want any copies to be created, say if I had a singleton for
example.

Should I make the copy constructor public or private? I don't plan on
defining it. Does it make a difference?

If I make it private, what happens to the assignment operator? Does it
try to use the undefined private copy constructor, or would it use the
default copy constructor?

If the undefined copy constructor were public, what happens to the
assignment operator? Will it try to use the undefined public copy
constructor or will it use the default? I would think in this case it
would have to try and use the public undefined copy constructor, but
then fail since it's undefined.

Thanks,

--John Ratliff
 
K

Karl Heinz Buchegger

John said:
What are the ramifications of creating a private copy constructor?

As with any other private thing:
Access is granted to member functions of that class only.
Every other code that is not a member function of that class doesn't have
access to that part of the class (the copy constructor).
Say I didn't want any copies to be created, say if I had a singleton for
example.

Should I make the copy constructor public or private? I don't plan on
defining it. Does it make a difference?

Sure.
If you make it public, then any other code can access that function ->
The compiler will happily compile that other code.
Only during the linking stage, the linker will notice that there is no
implementation for that function and will barf.

If you make it private, then no other code has the access rights to
use that constructor. -> The compiler will emit an error message
That leaves you with the special case of using the copy constructor
from one of the member functions of the very same class. They have
access rights and the compiler will happily encode calls to the copy
constructor in them. But again: Eventually the linking stage starts
and the linker will notice that the function is not implemented.
If I make it private, what happens to the assignment operator?

Nothing at all.
Does it
try to use the undefined private copy constructor, or would it use the
default copy constructor?

????
Assignment operator and constructors have nothing to do with each other.
Why do you think that op= uses a constructor for anything?

A constructor is used for creating objects.
When an op= runs, then the object has been created long before.
 
J

John Ratliff

Does it
????
Assignment operator and constructors have nothing to do with each other.
Why do you think that op= uses a constructor for anything?

A constructor is used for creating objects.
When an op= runs, then the object has been created long before.

Sorry, I was under the impression that the default assignment operator
used the default copy constructor.

Thanks,

--John Ratliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top