Copy constructor issue

Hi all,

We know copy constructor is required either of the following scenario :
1) when we try to intialize one object using another object.
2) When we try to pass an object by value to a function or
3) when we return an object by value from a function.
I have written the following code just to test all the scenarios the first one worked well. Second one also didn't throw any error but it didn't give the desired result

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include<iostream>

using namespace std;

class c_const
{
      int x;
      int y;
      public :
             c_const(int a, int b)
             {
                          cout <<"in constructor!!"<<endl;
                          x = a;
                          y = b;
                          
             }
             void print()
             {
              cout << "x  " << x << endl;
              cout << "y  " << y << endl;
             }
             ~c_const()
             {
                       cout << "detructed " << endl;
                       cout << "x_d " << x <<endl;;
                       cout << "y_d  " << y << endl;;
             }
             c_const( c_const & pp)
             {
                      cout <<"in copy constructor!!!"<< endl;
                      x = pp.x;
                      y = pp.y;
                      
             }
};

void func(c_const & cp)
{
     cout <<"hey ya "<<endl;
     cp.print();
     
 }
 
 c_const & func1  (int x , int y)
{
     c_const pq(5,6);
     cout << x << endl;
     cout << y << endl;
     return pq;    
}
int main()
{
    {c_const cc(10,20);
   // c_const cc1(cc);
    //c_const cc2 = cc1;
   // cc2 = cc1;
    cc.print();
   // cc1.print();
    func(cc);
    c_const rr(7,8);
    rr = func1(2,3);
     }
    system("pause");
    return 0;
}

}


When I am calling the function from main and passing an object by value to it copy constructor is not getting called same thing is happening when I am trying to return an object by value from a function as in the code.. please tell me where am I wrong?
Last edited on
Well you're returning a reference to that object, so the copy constructor is not going to be called. Change that function signature to return a copy, and then you'll see it being called.

Same goes for the func(). Change the parameter to be passed by value instead of by reference.
Last edited on
Thanks, for object passing as the argument is working but for returning an object from a function gives an error.

c_const func1 (int x , int y)
{
c_const pq(5,6);
cout << x << endl;
cout << y << endl;
return pq;
}
int main()
{
{c_const cc(10,20);
// c_const cc1(cc);
//c_const cc2 = cc1;
// cc2 = cc1;
cc.print();
// cc1.print();
func(cc);
//c_const rr(7,8);
/ c_const rr = func1(2,3);
}
system("pause");
return 0;
}

Wat's wrong?
What's the error? And why didn't you use code tags?
sorry..
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
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>

using namespace std;

class c_const
{
      int x;
      int y;
      public :
             c_const(int a, int b)
             {
                          cout <<"in constructor!!"<<endl;
                          x = a;
                          y = b;
                          
             }
             void print()
             {
              cout << "x  " << x << endl;
              cout << "y  " << y << endl;
             }
             ~c_const()
             {
                       cout << "detructed " << endl;
                       cout << "x_d " << x <<endl;;
                       cout << "y_d  " << y << endl;;
             }
             c_const( c_const & pp)
             {
                      cout <<"in copy constructor!!!"<< endl;
                      x = pp.x;
                      y = pp.y;
                      
             }
};

void func(c_const  cp)
{
     cout <<"hey ya "<<endl;
     cp.print();
     
 }
 
 c_const  func1  (int x , int y)
{
     c_const pq(5,6);
     cout << x << endl;
     cout << y << endl;
     return pq;    
}
int main()
{
    {c_const cc(10,20);
   // c_const cc1(cc);
    //c_const cc2 = cc1;
   // cc2 = cc1;
    cc.print();
   // cc1.print();
    func(cc);
    c_const rr(7,8);
    rr = func1(2,3);
     }
    system("pause");
    return 0;
}


It's giving error, no matching function for call to 'c_const::c_const(c_const)'
I just removed the '&' from return type of the function definition
Oh you're missing the const qualifier for your copy ctor. It should take a parameter of a const reference. Not just a reference.
Now it is working but again copy constructor is getting called .
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
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>

using namespace std;

class c_const
{
      int x;
      int y;
      public :
             c_const(int a, int b)
             {
                          cout <<"in constructor!!"<<endl;
                          x = a;
                          y = b;
                          
             }
             void print()
             {
              cout << "x  " << x << endl;
              cout << "y  " << y << endl;
             }
             ~c_const()
             {
                       cout << "detructed " << endl;
                       cout << "x_d " << x <<endl;;
                       cout << "y_d  " << y << endl;;
             }
             c_const(const c_const & pp)
             {
                      cout <<"in copy constructor!!!"<< endl;
                      x = pp.x;
                      y = pp.y;
                      
             }
};

/*void func(c_const  cp)
{
     cout <<"hey ya "<<endl;
     cp.print();
     
 }*/
 
 c_const   func1  (int x , int y)
{
     c_const pq(5,6);
     cout << x << endl;
     cout << y << endl;
     return pq;    
}
int main()
{
    {c_const cc(10,20);
   // c_const cc1(cc);
    //c_const cc2 = cc1;
   // cc2 = cc1;
    cc.print();
   // cc1.print();
    //func(cc);
    c_const rr(7,8);
    rr = func1(2,3);
     }
    system("pause");
    return 0;
}
And also what is the significance of 'const'? why was it throwing error without it?
The technical reason is a temporary object cannot bind to a non-const reference.

A more intuitive (?) reason is because you don't want to be changing that object, and marking objects as const is a good defensive programming technique.
Topic archived. No new replies allowed.