1

I have a MyPoly class where I implemented my own equal operator ( = ). When I try this code everything works fine and my implemented = is being called.

MyPoly mp = MyPoly(arr , 4); 

MyPoly copy;
copy = mp;

But when I write this:

MyPoly mp = MyPoly(arr , 4); 

MyPoly copy = mp;

It doesn't use my implemented = , and then when the destructor is called I get a run time error.

Can someone explain why these codes are different?

Bart
  • 19,692
  • 7
  • 68
  • 77
oopsi
  • 1,919
  • 3
  • 21
  • 28
  • 1
    I think in the second case, the copy constructor is used instead of the assignment operator. – vstm Sep 06 '12 at 11:51
  • 1
    I'm leaving, so I don't have time for a full answer, but this is due to the unfortunate fact that C++ uses the symbol `=` for both *assignment* (first case) and *initialization* (second case). – R. Martinho Fernandes Sep 06 '12 at 11:51

4 Answers4

8

This line

MyPoly copy = mp;

is a copy initialization, so it does not call the assignment operator (what you refer to as "equal operator"), but rather the copy constructor, which has signature

MyPoly(const MyPoly&); 

and is generated by the compiler unless you provide your own. As to the runtime error, you need to provide more code. But I could speculate that, since you have written your own assignment operator, you may be dealing with some dynamically allocated resources, in which case you should follow the rule of three and implement your own copy constructor and destructor. And if you have C++11 support, you should extend that into the rule of 5 and provide your own move copy constructor and move assignment operator.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

This

MyPoly copy = mp;

is not assignment, but copy initialization. It uses the copy constructor, not the assignment operator.

MyPoly copy(mp);

is direct initialization.

MyPoly copy;
copy = mp;

is assignment.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3
MyPoly mp = MyPoly(arr , 4); 

MyPoly copy = mp;

Calls copy c-tor, not operator =. It's copy initialization in both cases.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
-1
MyPoly copy = mp;

Is the same as:

MPoly copy(mp);

As in, it invokes the copy constructor instead of the assignment operator. Creating a copy constructor alongside an assignment operator is always a good idea.

Nola
  • 90
  • 2
  • http://stackoverflow.com/questions/11222076/why-is-copy-constructor-called-instead-of-conversion-constructor – Luchian Grigore Sep 06 '12 at 11:54
  • http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati – jrok Sep 06 '12 at 11:54
  • It's the same *in that* they both invoke the copy ctor. They differ in other respects. Folks from NOLA talk different anyways ;-) – Steve Jessop Sep 06 '12 at 12:12
  • I meant "equal on the terms that they both invoke the copy constructor", not absolutely equal. Inadequate and wrong wording of my thoughts :-(. – Nola Sep 07 '12 at 10:53