Deep copy and Shallow copy w.r.t Copy Constructor and Assignment Operator
-
Hi to Everyone. Its been a long time, I have been searching for an clear cut idea of ....shallow copy and deep copy with regards to Copy constructor and assignment operator. I search the web, but many places has been confused me...I know that the problem comes only when we do use of pointers...But, dont know exact situation and solution for it. when to use which one..ie in which situation to used which one.... Can anyone please help me understanding the concept better. A good example problem with relating to all these topic would be appreciated. May be could you please relate the example with the following situations 1. Default copy constructor 2. Default assignment operator 3. overloaded copy constructor 4. overloaded assignment operator Thanks in advance
----------------------------- I am a beginner
-
Hi to Everyone. Its been a long time, I have been searching for an clear cut idea of ....shallow copy and deep copy with regards to Copy constructor and assignment operator. I search the web, but many places has been confused me...I know that the problem comes only when we do use of pointers...But, dont know exact situation and solution for it. when to use which one..ie in which situation to used which one.... Can anyone please help me understanding the concept better. A good example problem with relating to all these topic would be appreciated. May be could you please relate the example with the following situations 1. Default copy constructor 2. Default assignment operator 3. overloaded copy constructor 4. overloaded assignment operator Thanks in advance
----------------------------- I am a beginner
There's no difference concerning this behavior between a copy constructor and an assignement operator, so my explanation is valid for both. The problem with a default constructor is that it simply copies byte by byte all the content of your class. This is ok for most of the cases when your class contains only plain data type for instance. Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy, both class instances will contain a pointer pointing at the same memory location. If you have a destructor like this:
MyClass::~MyClass()
{
if (myPointer)
{
delete myPointer;
myPointer = NULL;
}
}Then you will end up in big troubles once one of the class instances gets destroyed because its destructor will be called and you will end up with a pointer pointing to memory which has been release in your other instance. So, what you have to do instead is allocate a new pointer and copy the content when you copy the class:
MyClass::MyClass(const MyClass& copy)
{
myPointer = new ....; // Allocate a new pointer
// Copy the content of the pointer based on what is in "copy"
......
}Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
There's no difference concerning this behavior between a copy constructor and an assignement operator, so my explanation is valid for both. The problem with a default constructor is that it simply copies byte by byte all the content of your class. This is ok for most of the cases when your class contains only plain data type for instance. Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy, both class instances will contain a pointer pointing at the same memory location. If you have a destructor like this:
MyClass::~MyClass()
{
if (myPointer)
{
delete myPointer;
myPointer = NULL;
}
}Then you will end up in big troubles once one of the class instances gets destroyed because its destructor will be called and you will end up with a pointer pointing to memory which has been release in your other instance. So, what you have to do instead is allocate a new pointer and copy the content when you copy the class:
MyClass::MyClass(const MyClass& copy)
{
myPointer = new ....; // Allocate a new pointer
// Copy the content of the pointer based on what is in "copy"
......
}Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++Thanks a lot for your valuable reply Got some more doubts again.
Cedric Moonen wrote:
The problem with a default constructor is that it simply copies byte by byte all the content of your class.
What does the code of default constrictor is...what I mean is what is the code present in the default constructor?
Cedric Moonen wrote:
Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy
Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?
----------------------------- I am a beginner
-
Thanks a lot for your valuable reply Got some more doubts again.
Cedric Moonen wrote:
The problem with a default constructor is that it simply copies byte by byte all the content of your class.
What does the code of default constrictor is...what I mean is what is the code present in the default constructor?
Cedric Moonen wrote:
Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy
Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?
----------------------------- I am a beginner
See Copy constructor and default constructor[^]
“Follow your bliss.” – Joseph Campbell
-
Thanks a lot for your valuable reply Got some more doubts again.
Cedric Moonen wrote:
The problem with a default constructor is that it simply copies byte by byte all the content of your class.
What does the code of default constrictor is...what I mean is what is the code present in the default constructor?
Cedric Moonen wrote:
Now, if you have a class which contains a pointer and then if you copy your class using a shalow copy
Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?
----------------------------- I am a beginner
hrishiS wrote:
What does the code of default constrictor is...what I mean is what is the code present in the default constructor?
If you don't provide any constructor yourself, the default constructor will simply assign all variables, similarly as you would do something like this:
MyClass::MyClass(const MyClass& copy)
{
member1 = copy.member1;
member2 = copy.member2;
....
}So, it means that if your class contains a pointer, this will result in something like this:
MyClass::MyClass(const MyClass& copy)
{
myPointer = copy.myPointer;
}And of course this is not nice because both of your instances will have a pointer pointing at the same location and there's no way anymore to know which of the two has ownership of the pointer (so, which one is responsible for deleting it). And if you destroy that pointer in the class destructor, then you end up with the problem I described earlier: one instance will end up with a pointer pointing to memory which has been released (and thus which can contain corrupted data). Furthermore, you can also encounter another problem than with the one you have when manipulating pointers. If one (or more) member of your class is an object that cannot be copied (because its copy constructor has been made private for instance), then your code won't even compile because there's no way for the default constructor of your class to copy the object (as the copy constructor is private). Thus, the only way to be able to copy your class is to provide a copy constructor that instead of calling the copy constuctor of your member, it will create an new instance of this member and initialize it properly.
hrishiS wrote:
Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?
It is assigning all members from one class to another as I described at the begining of this message.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
hrishiS wrote:
What does the code of default constrictor is...what I mean is what is the code present in the default constructor?
If you don't provide any constructor yourself, the default constructor will simply assign all variables, similarly as you would do something like this:
MyClass::MyClass(const MyClass& copy)
{
member1 = copy.member1;
member2 = copy.member2;
....
}So, it means that if your class contains a pointer, this will result in something like this:
MyClass::MyClass(const MyClass& copy)
{
myPointer = copy.myPointer;
}And of course this is not nice because both of your instances will have a pointer pointing at the same location and there's no way anymore to know which of the two has ownership of the pointer (so, which one is responsible for deleting it). And if you destroy that pointer in the class destructor, then you end up with the problem I described earlier: one instance will end up with a pointer pointing to memory which has been released (and thus which can contain corrupted data). Furthermore, you can also encounter another problem than with the one you have when manipulating pointers. If one (or more) member of your class is an object that cannot be copied (because its copy constructor has been made private for instance), then your code won't even compile because there's no way for the default constructor of your class to copy the object (as the copy constructor is private). Thus, the only way to be able to copy your class is to provide a copy constructor that instead of calling the copy constuctor of your member, it will create an new instance of this member and initialize it properly.
hrishiS wrote:
Could you please elaborate this, what exactly is meant by copying using shallow copy? Does this mean copying with the default copy constructor what we have?
It is assigning all members from one class to another as I described at the begining of this message.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++Cedric Moonen wrote:
If you don't provide any constructor yourself, the default constructor will simply assign all variables,
But the default constructor doesn't have any parameter with it...So in that case to which value the member variable will get initialized to?...
Cedric Moonen wrote:
It is assigning all members from one class to another as I described at the begining of this message.
Does it meant that shallow copy is member to member copying of each member variable one by one...? is it,...Copy Constructor is doing shallow copy? In that case, if we dont provide an copy constructor does the default copy constructor provides deep copy?
----------------------------- I am a beginner
-
Cedric Moonen wrote:
If you don't provide any constructor yourself, the default constructor will simply assign all variables,
But the default constructor doesn't have any parameter with it...So in that case to which value the member variable will get initialized to?...
Cedric Moonen wrote:
It is assigning all members from one class to another as I described at the begining of this message.
Does it meant that shallow copy is member to member copying of each member variable one by one...? is it,...Copy Constructor is doing shallow copy? In that case, if we dont provide an copy constructor does the default copy constructor provides deep copy?
----------------------------- I am a beginner
hrishiS wrote:
But the default constructor doesn't have any parameter with it...So in that case to which value the member variable will get initialized to?...
We are talking about the default copy constructor here. The default copy constructor receives the object from which to copy.
hrishiS wrote:
Does it meant that shallow copy is member to member copying of each member variable one by one...? is it,...Copy Constructor is doing shallow copy?
Yes, that's what I showed you in my previous reply: a default copy constructor simply assigns all members one by one (in fact, it is using the copy constructor of each member to create them and not the assignement operator, but this is not important in the discussion here). So, in that sense a default copy constructor is doing shallow copy.
hrishiS wrote:
In that case, if we dont provide an copy constructor does the default copy constructor provides deep copy?
No, I just said the opposite: a default copy constructor does not make a deep copy. How could it do it ? You are the one how wrote the clas, so you know you have to copy some specific members (like pointers). The compiler doesn't know anything of that, he can simply assign all members from one object to the other. How can he know that for a pointer he has to allocate a new pointer and copy the content of the pointer ?
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++