Using copy constructor

Here is my code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std ;

class A {
    private :
    int a ;

    public:
        A() {
            a = 0 ;
            cout << "Default constructor" << endl ;
        }

        A(int x ){
            cout <<" Parameterized Constructor " << endl ;
            a = x ;
        }

        int getA(){
            return a ;
        }

        //Copy Constructor
        A(const A &obj) {
            cout <<" Copy Constructor " << endl ;
            *this = obj ;
            //  a = obj.a ;
        }

        void setA(int c){
            a = c ;
        }
};

int main()
{
     A obj1;
     cout << obj1.getA() << endl; //default constructor

     A obj2(10);
     cout << obj2.getA() << endl; //parameterized constructor

     A obj3 = obj2 ;
     cout << obj3.getA() << endl ; 
     obj2.setA(90); // changes in obj2 are not reflected in obj3
     cout << obj3.getA() << endl ;

     A obj4(obj2);
     cout << obj4.getA() << endl; 
     obj2.setA(25);//changes in obj2 are not reflected in obj4
     cout << obj4.getA() << endl;

     return 0 ;
}

If the copy constructor is not defined by the user , the default copy constructor is invoked. Is it so ?
Does the new Object reflect changes made in the copied object ? Here , it doesn't
If copy constructor is doing the same thing as the default copy constructor , then why do we use it ? what are its advantages ? I could only think of one , that we can copy some class variables instead of all variables that happens by default .

Any ideas will be appreciated..
In your example the copy constructor is in fact equivalent to default (implicitly created by the compiler) copy constructor because inside its body it calls the default copy assignment operator

1
2
3
4
5
6
        //Copy Constructor
        A(const A &obj) {
            cout <<" Copy Constructor " << endl ;
            *this = obj ; //<== here the default copy assignment operator is called
            //  a = obj.a ;
        }


So there is no any need in your copy constructor.
Last edited on
If the copy constructor is not defined by the user , the default copy constructor is invoked. Is it so ?


Yes.

Does the new Object reflect changes made in the copied object ? Here , it doesn't



If you xerox a document, then scribble on the original afterwards... the copy will not have the same scribbles.

Once the copy is made, it is its own unique object, and changes to the original will not affect the copy at all.

If copy constructor is doing the same thing as the default copy constructor , then why do we use it ? what are its advantages ?


The main purpose for copy constructors is to "deep copy" allocated memory or resources, since the compiler-provided copy ctor will only do a shallow copy. This is very important in classes which do their own memory/resource management.

I could only think of one , that we can copy some class variables instead of all variables that happens by default .


That would not be an advantage, really. A copy constructor should copy everything necessary to make the copy appear identical to the original. If you design your copy ctor differently, then it will lead to very unexpected and confusing code.


EDIT: GRAH vlad! That's 3 times in a row! gaaaah! XD XD
Last edited on
The main purpose for copy constructors is to "deep copy" allocated memory or resources, since the compiler-provided copy ctor will only do a shallow copy. This is very important in classes which do their own memory/resource management.


Can you please explain deep copy and shallow copy ?

http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

According to this link :
The shallow copy only the references are copied , however in case of deep copy all the values are copied .
Doesn't that mean that if I have a shallow copy the changes within the copy will be seen in the original object ?
Last edited on
1
2
3
4
5
6
7
8
int* buffer = new int[100];

// shallow copy of 'buffer' -- what the default copy ctor does:
int* shallow = buffer;

// deep copy of 'buffer' -- what a class will probably have to do if managing its own memory
int* deep = new int[100];
std::copy( buffer, buffer + 100, deep );


The default copy ctor cannot do a deep copy automatically because it cannot know how the buffer was allocated, how large it needs to be, or how many elements need to be copied.
thanks for replying..
Hi,

A good place to implement a copy constructor is when your class has a string variable or any other dynamically allocated memory.

As Disch mentioned, if the default copy constructor is called, the new object's pointer variables will contain the same address as that of the original object's pointer variable.
So, if you make changes in one, it will reflect in the other and that sort of defeats the purpose of copying.

E.g:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class str
{
  char *s;
  str()
  {
    s=new char[256];
  }
  ~str()
  {
    delete s;
  }
  str (str& _str)
  {
    s = new char[256];
    strcpy(s, _str.s);
  }
};


If the copy constructor wasn't there and then the original copy constructor wouldn't have allocated a new memory and copied the string. Instead, it would have assigned the address contained in s to the new object.
So, if you changed the value of s in one object, it would have reflected in the new object too.
So here it is necessary to explicitly define a copy constructor.
Last edited on
Topic archived. No new replies allowed.