Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am now tired but still not able to understand Copy constructor and assignment operator clearly.
And at last, i am here, in one of my best site, in a hope that i'll clear all my concepts.

Now coming to the point, i know default copy constructor do shallow copying but when we copy object using assignment operator then will it become a deep copy...???

Let us suppose, we have a class, MyClass and a constructor is defined in it without any argument, and main is coded as:

C++
MyClass c1("Welcome");
MyClass  c2;
c2 = c1;


So, the 3rd line "c1=c2" is deep copy??
And if it is also a shallow copy that what's the difference between this "c2=c1" and "MyClass c2(c1)"(Simple copy constructor).

And what will happen if i'll edit it as:
C++
MyClass c1("Welcome");
MyClass  c2 = c1;


Thanks in advance...:-)
Posted
Updated 13-Mar-14 8:30am
v3
Comments
Andreas Gieriet 13-Mar-14 16:46pm    
Show us your complete MyClass definition. Without that, we cannot answer your question. Especially, what are the data member and how are they initialized.
Cheers
Andi

Whether you are calling a default copy constructor or assignment operator the compiler can only create a shallow copy by default. A shallow copy means that each member of the class is copied individually using the assignment operator for that member. This is important in the case of pointers because the pointer is copied but what it points to is not. If memory has been allocated and used this creates a problem.

C++
#include <string>
#include <iostream>
using namespace std;

// Use pointer to memory for string
class myClass 
{
public:
	myClass(const char *cstring) { mystring = new char[30]; strcpy(mystring, cstring);};
	myClass() : mystring(NULL) {};
	~myClass() {delete [] mystring;};

	char *mystring;
};

// Use std::string class
class myClass2 
{
public:
	myClass2(const char *cstring) { mystring = cstring;};
	myClass2() : mystring("") {};
	~myClass2() {};

	string mystring;
};

int main()
{

myClass c1("Welcome");
myClass  c2;
c2 = c1; 
// Shallow copy - c2::mystring pointer is assigned same value as c1::mystring 
// pointer so points to same memory - DANGER
// This is deceptive if you examine c1 and c2 in the debugger because they both seem
// to contain "Welcome" - not so - they both have a pointer which points to the same
// memory.

// This must be carefully managed. For example:
//#define TEST
#ifdef TEST
myClass *pc2 = new myClass;
*pc2 = c1;

cout << c1.mystring << endl; // All good.
cout << pc2->mystring << endl; // All good.
delete pc2;
cout << c1.mystring << endl; // Problem - why?
// Consequence of shallow copy.
#endif

myClass c1a("Welcome");
myClass  c2a = c1a; // Same as above - different construct same result.


// Now using myClass2
myClass2 c1_2("Welcome");
myClass2  c2_2;
c2_2 = c1_2; 
// Shallow copy - but c2::mystring is now a copy of c1::mystring because
// of assignment operator for std::string.

// Now
//#define TEST2
#ifdef TEST2
myClass2 *pc2_2 = new myClass2;
*pc2_2 = c1_2;

cout << c1_2.mystring << endl; // All good.
cout << pc2_2->mystring << endl; // All good.
delete pc2_2;
cout << c1_2.mystring << endl; // No Problem.
#endif


 return 0;
 }


This is a very good expose:
http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/[^]
 
Share this answer
 
v8
Comments
Atul Khanduri 18-Mar-14 15:50pm    
Thanks for the solution and apologies for late reply as i was very busy.
BTW, i want to ask that is there any other way to deep copying object other than overloading assignment operator.??
[no name] 18-Mar-14 20:03pm    
You're welcome. You can overload assignment operator or implement copy constructor.
http://getyouralgorithm.blogspot.com.au/2012/06/shallow-vs-deep-copies-all-about-object.html
Atul Khanduri 19-Mar-14 5:29am    
Okay, got it...
Thanks a lot...:)
Consider the following C++ program.
C++
#include<iostream>
#include<stdio.h>
using namespace std;
class Test
{
public:
   Test() {}
   Test(const Test &t)
   {
      cout<<"Copy constructor called "<<endl;
   }
   Test& operator = (const Test &t)
   {
      cout<<"Assignment operator called "<<endl;
   }
};
int main()
{
  Test t1, t2;
  t2 = t1;
  Test t3 = t1;
  getchar();
  return 0;
}

Output:
Assignment operator called
Copy constructor called

Copy constructor is called when a new object
is created from an existing object, as a copy
of the existing object (see this G-Fact). And
assignment operator is called when an
already initialized object is assigned a new
value from another existing object.
C++
t2 = t1;  // calls assignment operator, same as "t2.operator=(t1);"
Test t3 = t1;  // calls copy constructor, same as "Test t3(t1);"
 
Share this answer
 
v2
Comments
Atul Khanduri 14-Mar-14 2:16am    
You wrote "see this G-Fact", but actually there is no link regarding G-fact in your solution...

And this solution seems to be copied from http://www.geeksforgeeks.org/copy-constructor-vs-assignment-operator-in-c/, isn't it...??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900