Sign in to follow this  

Preferred way of copying C++ classes?

This topic is 3098 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
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:
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
Quote:
Original post by SiCrane
Dude, quit while your still ahead. That post made absolutely no sense.


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


Link to post
Share on other sites
Quote:
Original post by theequal
my 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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by theequal
my 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

Share this post


Link to post
Share on other sites
I think we're done here, but one final point -- incorrect use of terminology may not have really mattered this time, but given it was simply noted in a friendly fashion I think declaring it arrogant is a little over-the-top -- correct terminology may well mean the difference between getting a sensible answer or not in future cases.

Non-native speakers often appreciate having flaws in their use of the language politely corrected so that they can use the correct term in future; don't take offence at a friendly correction, but instead just take the knowledge on board and move on. [smile]

Share this post


Link to post
Share on other sites

This topic is 3098 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