copy constructor

closed account (9605fSEw)
class fraction
{
private:
int *num;
int *denom;
public:
fraction(int =0, int =1); // constrctr
fraction(const fraction &); //copy constrctr
~fraction();
}

in main()

{
int n, d;
----//get values of n and d from user
fraction f(n, d); //automatci call to constructor ...UNDERSTOOD
fraction *pf = new fraction (n,d); // understood the use of new operator here
//and also that the cnstrctr will be called

fraction f2 =*pf;// txt book says tht f2 gts a copy thru copy constrct???
fraction *pf2 = new fraction(f); //same is claimed for this..understood a
bit..how..but bttr if smone can expln

}
fraction f2 =*pf;
You create a new object named f2 that will have as member values the same values as the *pf object. So you copy from one to the other. It calls the copy constructor.

fraction *pf2 = new fraction(f);
It is the same as above with the difference that now you use a pointer to create a dynamic object of the class fraction that will have the same data as the previous 'f' object.

Hope this helps
closed account (9605fSEw)
Thanks Mitsakos..
so the bottom line can be picked up as...when an object is assigned using another object of the same class...as in the above examples...and since in first two assignment, the assignment is done by providing values directly..(not using a object as in later examples)...i think almost i got..right..?

and also...one more question...what if in the same example...the copy cnstrctr wouldn't exist there..
Yes, when you assign to an object using another object of the same class. Also it is called when you pass an object as an argument in a function because you have an assignement to a new object also.

The copy constructor is automatically created and assigns each value individually to the new object. You make your own copy constructor when you want another behaviour than the default.

Some times it is necessary to have a copy constructor. For example when you have a pointer in the class and you want to pass it to a function as a variable.
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
#include <iostream>
using namespace std;

class myClass{
public:
   int* ptr;
   myClass();
   //myClass(myClass &obj);
   ~myClass();
};
myClass::myClass(){
   ptr = new int();
   *ptr = 5;
   cout << "Constructor...\n";
}
myClass::~myClass(){
   delete ptr;
   cout << "Destructor...\n";
}

void anyFunction(myClass funcObj);
int main(){
   myClass object;
   cout << *(object.ptr) << '\n';
   anyFunction(object);
   return 0;
}
void anyFunction(myClass funcObj){
   cout << *(funcObj.ptr) << '\n';
}


If you run this you will see that you have one time a call to the constructor and two times to the destructor. That means that you try to delete the same object two times (illegal). This happens because the default copy contructor will copy the value of "ptr", which is a memory address and not the value of the object (int) pointed by this memory address.
So you must have a copy constructor to make a new int to the pointer:
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
#include <iostream>
using namespace std;

class myClass{
public:
   int* ptr;
   myClass();
   myClass(myClass &obj);
   ~myClass();
};
myClass::myClass(){
   ptr = new int();
   *ptr = 5;
   cout << "Constructor...\n";
}
myClass::myClass(myClass &obj){
   ptr = new int();//The copy constructor creates a new int to the pointer
   *ptr = *(obj.ptr); //and then assigns the values
   cout << "Copy constructor...\n";
}
myClass::~myClass(){
   delete ptr;
   cout << "Destructor...\n";
}

void anyFunction(myClass funcObj);
int main(){
   myClass object;
   cout << *(object.ptr) << '\n';
   anyFunction(object);
   return 0;
}
void anyFunction(myClass funcObj){
   cout << *(funcObj.ptr) << '\n';
}


HTH
Last edited on
Topic archived. No new replies allowed.