# Preferred way of copying C++ classes?

## Recommended Posts

theequal    145
Just out of curiosity, what do most C++ people prefer to do when they need to copy a C++ class that contains allocated memory? Would they rather overload the assignment operator, or would they add an explicit function to handle it? MyClass A; // contains allocated memory MyClass B; // copy by assignment (overloaded operator) A = B; OR: // copy by explicit function A.Copy(B); I would think adding a Copy() function might be more acceptable, because it basically lets you know what's happening behind the scenes. To me, the assignment operator is more of a mystery, if you don't know what's going on (are we copying pointers only, or doing a deep copy?).

##### Share on other sites
SiCrane    11839
It's by far more idiomatic to use the assignment operator. The compiler's going to generate one for you if you don't, so you might as well make sure that if one exists, it works properly.

##### Share on other sites
Hodgman    51234
Agree that the standard assignment operator / copy constructor is the way to go. When in doubt, do what the standard library does E.g. assigning one vector to another does a deep copy.
Quote:
 Original post by theequalI would think adding a Copy() function might be more acceptable, because it basically lets you know what's happening behind the scenes. To me, the assignment operator is more of a mystery, if you don't know what's going on (are we copying pointers only, or doing a deep copy?).
If you're going to go down this path, then you should make the object uncopyable except via your new method:
class Thing{public:  void Copy( const Thing& other )  {    this.buffer = copy other.buffer ...  }private:  Thing( const Thing& );//not implemented  Thing& operator=( const Thing& );//not implemented};

##### Share on other sites
theequal    145
Quote:
Original post by Hodgman
Agree that the standard assignment operator / copy constructor is the way to go. When in doubt, do what the standard library does E.g. assigning one vector to another does a deep copy.
Quote:
 Original post by theequalI would think adding a Copy() function might be more acceptable, because it basically lets you know what's happening behind the scenes. To me, the assignment operator is more of a mystery, if you don't know what's going on (are we copying pointers only, or doing a deep copy?).
If you're going to go down this path, then you should make the object uncopyable except via your new method:*** Source Snippet Removed ***

Thanks, that's sounds like a good idea. By making the assignment private, that will generate compiler errors, so the user won't get caught trying to copy it like that?

##### Share on other sites
dmatter    4826
Quote:
 Original post by theequalTo me, the assignment operator is more of a mystery, if you don't know what's going on (are we copying pointers only, or doing a deep copy?).
Part of your job when designing a class involves hammering out semantics, you have to specify what it means to assign one instance of your class to another. The semantics are recorded, somehow, in the documentation so people can be certain what the effects of assignment are in any given circumstance. In C++ we do not expect that the semantics of assignment are the same for all objects but we do expected that it is sensible.

It might well be the case that certain types of assignments between objects are desired but they carry some form of gotcha (perhaps the semantics of A = B, A = C and A = D are all similar but the semantics of A = E are widly different and/or risky); in that case it might well make sense to use a separate function so that the user has to expend a few more mental 'brain cycles' to make the assignment happen.

##### Share on other sites
Hodgman    51234
Quote:
 Original post by theequalThanks, that's sounds like a good idea. By making the assignment private, that will generate compiler errors, so the user won't get caught trying to copy it like that?
Yep it's private so the compiler will complain if someone tries to use it. Your class can still use it internally without the compiler complaining though, but if you don't actually implement that function anywhere, then the linker will complain about these cases too ;)

This is pretty much the standard C++ way of making a "non-copyable" class.

##### Share on other sites
Oluseyi    2103
Quote:
 Original post by theequalJust out of curiosity, what do most C++ people prefer to do when they need to copy a C++ class that contains allocated memory?

Just by way of semantic pedantry, you're not copying the class. You're copying an instance of the class, an object. A class is a template for creating objects, and in C++ ceases to exist in any user-addressable or -modifiable way at runtime.

Proper use of terminology helps people understand you better. At first, reading the thread title, I was wondering why it mattered how you copied the text of the class definition... [smile]

##### Share on other sites
theequal    145
Quote:
 Original post by OluseyiProper use of terminology helps people understand you better. At first, reading the thread title, I was wondering why it mattered how you copied the text of the class definition... [smile]

##### Share on other sites
jpetrie    13104
Quote:

Which doesn't mean you were correct in your use of terminology.

##### Share on other sites
theequal    145
Quote:
Original post by jpetrie
Quote:

Which doesn't mean you were correct in your use of terminology.

Enough said.

[Edited by - theequal on June 19, 2009 3:28:34 PM]

SiCrane    11839

##### Share on other sites
theequal    145
Quote:

Yes, I know, this is pointless, my question has already been answered, gave out thanks, so I don't understand why the lecturing
on a subject line of all things.

Anyway thanks again.

##### Share on other sites
Zahlman    1682
Quote:
 Original post by theequalmy question has already been answered, gave out thanks, so I don't understand why the lecturing on a subject line of all things.

At gamedev.net, we deal in education, not question-answering.

##### Share on other sites
Hodgman    51234
Quote:
Original post by Zahlman
Quote:
 Original post by theequalmy question has already been answered, gave out thanks, so I don't understand why the lecturing on a subject line of all things.

At gamedev.net, we deal in arrogant pedantry, not question-answering.
Fixed =D