Public Group

# 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.

## 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 on other sites
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 on other sites
Quote:
 Original post by EndarThe 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 EndarWill 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 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.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633388
• Total Posts
3011620
• ### Who's Online (See full list)

There are no registered users currently online

×