By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

copy constructors and the assignment operator

P: n/a
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
Aug 10 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John Ratliff wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Aug 10 '05 #2

P: n/a
>>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.


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

Thanks,

--John Ratliff
Aug 10 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.