Jump to content
  • Advertisement
Sign in to follow this  
theequal

Preferred way of copying C++ classes?

This topic is 3292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
};

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:

Google understand me. :)

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 3292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!