Jump to content
  • Advertisement
Sign in to follow this  
Endar

Copy constructors and data members, C++

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

The default copy constructor usually just uses memcpy to copy the data members (int, float) of a class, right? But how about the more complicated data members?
class ISceneGraphNode
{
protected:

	util::list<ISceneGraphNode*> m_childList;

};


Will something like this call the copy constructor of list<>? Or will it just use memcpy to copy the data (pointer data) straight over?

Share this post


Link to post
Share on other sites
Advertisement
The default copy constructor will call the default copy constructor of its members as well, no memcpy at all. For pod types the default copy constructor basically behaves like memcpy.

For your example, the lists copy constructor will be called and a perfect valid copy will be created. No need to do it yourself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
The default copy constructor usually just uses memcpy to copy the data members (int, float) of a class, right?


Not memcpy you mean bitwise copy and not always, it depends on what kind of type it is:

If it is a POD-class type then the default implicitly defined does a bitwise copy (this follows the semantics of struct copying in C)

if it is a NON POD-class type then the default implicitly defined does a member-wise copy.

Quote:
Original post by Endar
Will something like this call the copy constructor of list<>? Or will it just use memcpy to copy the data (pointer data) straight over?


Your example is a NON POD-class type, it will result in a member-wise copy hence invoke std::list's user-defined copy constructor.

You should relize that it will not make a copy of instances referred to by the pointers stored in a standard library container, so in a sense it is still a shallow copy as you are only copying pointers that refer to the same instances.

[Edited by - snk_kid on August 26, 2005 5:13:34 AM]

Share this post


Link to post
Share on other sites
Thanks. I did know this, but I'm having a small period of my brain not working.

I actually had to sit down and try to remember how virtual functions worked when called the derived and base class versions of the same function.

Wierd.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!