Operator Overloading and Copy Constructor

I have the following c++ code.
My problem here is that copy constructor also gets invoked when i try to call the assignment operator...

In the below source code in the main funtion in line no 36 i have tried to assign the assignment operator... but if u observe the output then, first the copy constructor is called and then the assignment operator is invoked after the copy constructor...
could any one thus let me know where i made the mistake...


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
//overloading the assignment operator
1. #include<iostream>
2. #include<conio.h>
3. using namespace std;
4. class alpha
5. {
6. private:
7.	int data;
8. public:
9.	alpha()	{}
10.	alpha(int d)
11.	{
12.         data=d; 
13.	 cout<<"\n it is me";
14.         }
15.	alpha(alpha& a)
16.	{
17.		data= a.data;
18.		cout<<"\nCopy constructor invoked\n"<<data;
19.	}
20.	void display() 	{cout<<data<<"\n";}
21.        //now overload the operator
22.	alpha operator=(alpha a)
23.	{
24.		data= a.data/2;
25.		cout<<"\nAssignment operator invoked"<<data;
26.		//--return alpha(data);
27.		return *this;
28.	}
29. };
30.
21. int main()
32. {
33.	alpha a1(36);
34.	getch();
35.	alpha a2;
36.	a2=a1;
37.	getch();	
38.	cout<<"\na2=";
39.	getch();	
40.	a2.display();
41.	getch();	
42.	alpha a3(a2); //this doesn't invoke = where as constructor is fired
43.	getch();	
44.	cout<<"\na3="; a3.display();
45.	getch();
46.	return 0;
47. }

closed account (z05DSL3A)
Your assignment operator should be

edited: was alpha operator=(const alpha& a), see next few posts
1
2
3
4
5
6
7
8
9
alpha& operator=(const alpha& a)
{
     // Check for assignment to self (of the form a = a).
     if(this == &a)
     { 
         return *this;
     }
     ...
}


As it is you are passing an object into the the function so one has to be created.
Last edited on
As Grey Wolf points out, the paremeter to the assignment operator should be const reference. The same applies to the copy constructor. In addition, the return value from the assignment operator should be a reference to the object itself. I also recommend declaring the constructor that takes an int explicit. The declarations becomes:

1
2
3
4
9.	alpha()
10.	explicit alpha(int d)
15.	alpha(const alpha& a)
22.	alpha& operator=(const alpha& a)


Your assignment operator should make the assigned object equal to the right hand object. It should set data to o.data. If that is not the behavior you want, you do not want an assignment operator at all. It's not good practice to create an assignment operator that does something different. If the only thing you do in the assignment is to set data = o.data, you don't need to check if the object is assigned to itself. However it's good practice to always do it.

I don't think any of this actually helps you with your question.

The answer is very simple:
 
alpha a3(a2);

The line above should call the copy constructor. The same is true for this:
 
alpha a3 = a2;


If you want to use the assignment operator you have to do this:
1
2
alpha a3;
a3 = a2;


However, this should not matter to you, because if you design these operations propertly, the results should be equivalent.
Last edited on
closed account (z05DSL3A)
Ooops, could have sworn I put a reference on the return. It was early and I was only halfway through my first cup of coffee.

I must admit, from the first line of the question, I went straight to the assignment operator parameters…then didn’t go much further


hi everyone,
Thanks for the help,
but even after changing the parameter to the following operator function the copy constructor is being evoked.
ropez,
the line 36 should call the assignment operator only after the following modification to the code... but the thing is that first assignment operator is being called and secondly the copy constructor is invoked when it is called in the following way
1
2
3
4
            alpha a1(36);
	getch();
	alpha a2;
            a2=a1;


1
2
3
4
5
6
7
8
alpha operator=(alpha& a)
	{
		//data= a.data/2; 
		data= a.data;
		cout<<"\nAssignment operator invoked"<<data;
		//--return alpha(data);
		return *this;
	}


and this code //data= a.data/2; was used in my first post just to check whether the copy construction was being called or the assignment operator was called.


Ok. I see now. I thought you ment line 42, because you had a comment there. In line 36, the assignment operator is called, as you expected. But you also got a call to the copy constructor which you didn't expect.

In this case, the answer is really to make both the parameter AND the return value from the assignment operator a reference (&).

Declare the four functions as in my first post, and you shouldn't have a problem.
Last edited on
1
2
3
4
5
6
	alpha operator=(alpha& a)
	{
		data= a.data;
		cout<<"\nAssignment operator invoked"<<data;
		return *this;
	}

is this correct??
Last edited on
closed account (z05DSL3A)
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
#include<iostream>
#include<conio.h>

 using namespace std;
 class alpha
 {
 private:
    int data;
 public:
    alpha()
    {
         cout<<"Default constructor invoked: " << endl;
    }

    explicit alpha(int d)
    {
         data=d;
         cout<<"Int constructor invoked: " << endl;
    }
    
    alpha(const alpha& a)
    {
        data= a.data;
        cout<<"Copy constructor invoked: "<<data << endl;
    }
    
    void display() 
    {
        cout<<data<<"\n";
    }
        
    //now overload the operator
    alpha& operator=(const alpha& a)
    {
        data= a.data/2;
        cout<<"Assignment operator invoked"<<data << endl;

        return *this;
    }

 };

 int main()
 {
    cout << "Calling Int Constructor:..." ;
    alpha a1(36);
    cout << "Calling default Constructor:..." ;
    alpha a2;
    cout << "Calling assignment operator:..." ;
    a2=a1;
    cout << "Calling copy Constructor:...";
    alpha a3(a2);
    cout << "a1 = ";
    a1.display();
    cout << "a2 = ";
    a2.display();
    cout << "a3 = ";
    a3.display();
    cout << "Finish:";
    cin.get();

    return 0;
 }

Output:
1
2
3
4
5
6
7
8
9
Calling Int Constructor:...Int constructor invoked:
Calling default Constructor:...Default constructor invoked:
Calling assignment operator:...Assignment operator invoked
18
Calling copy Constructor:...Copy constructor invoked: 18
a1 = 36
a2 = 18
a3 = 18
Finish:

Last edited on
Grey Wolf,
Thanks a lot mate...
i am really relieved that it worked...
thanks again
parag
closed account (z05DSL3A)
Thanks must also go to Ropez. :0)
Topic archived. No new replies allowed.