Preferred way of copying C++ classes?

Started by
13 comments, last by jbadams 14 years, 10 months ago
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?).
Advertisement
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.
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 theequal
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?).
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};
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 theequal
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?).
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?

Quote:Original post by theequal
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?).
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.
Quote:Original post by theequal
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?
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.
Quote:Original post by theequal
Just 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]
Quote:Original post by Oluseyi
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]


Google understand me. :)

http://www.google.com/search?hl=en&q=copying+C%2B%2B+classes%3F&aq=f&oq=&aqi=

Quote:
Google understand me. :)

Which doesn't mean you were correct in your use of terminology.
Quote:Original post by jpetrie
Quote:
Google understand me. :)

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]

This topic is closed to new replies.

Advertisement