copy constructor and operator =

This is a discussion on copy constructor and operator = within the C++ Programming forums, part of the General Programming Boards category; I've got a program that I believe uses correct syntax, however in Visual studio 2005 it compiles fine, and in ...

Try making the parameter to your operator= a const Matrix&, since you shouldn't be modifying the passed in value anyway. You will have to make any other methods you use in operator= const correct (you should already be making all your methods const correct).

EDIT: oh sorry guys, i posted that code just before I tried switching my parameters correctly to const when the function does not change the parameter passed in. now everything works correctly, no warnings or errors, and my program runs correctly. Thanks Daved especially, and everyone else for your input.

Why does that have any effect? Within the operator=() function, the parameter m is not used to call a const member function, so as far as I can tell, whether you make it const or not should have no effect on whether the function executes error free.

linucksrox,

Your operator=() should not return void if you want to be able to write:

m1 = m2 = m3;

which is equivalent to:

m1 = (m2 = m3);

Being able to do that is generally provided for when overloading operators that can be chained together like that. In the case of operator=(), you want to return a reference to the left hand side, which will be: *this

Why does that have any effect? Within the operator=() function, the parameter m is not used to call a const member function, so as far as I can tell, whether you make it const or not should have no effect on whether the function executes error free.

The other way round... no non-const member function is called, so making it a const reference will save you a lot of trouble. You cannot pass a const Matrix if the operator only takes a non-const reference. And he doesn't need a non-const reference, so it should be const.

The error seems to indicate that the compiler cannot find a matching operator= function to execute:

no match for 'operator='

The only way that could be the case is if the argument is a const object; and the only way the argument could be a const object is if operator+ returns a const object. However, as far as I can tell, when the operator+() returns-by-value the copy constructor is called, and it creates a non-const object, so I don't get it.

I don't see how that can be correct. Returning void from operator=() prevents you from writing chain statements like this:

I stand corrected.

Originally Posted by 7stud

The error seems to indicate that the compiler cannot find a matching operator= function to execute:

The only way that could be the case is if the argument is a const object; and the only way the argument could be a const object is if operator+ returns a const object. However, as far as I can tell, when the operator+() returns-by-value the copy constructor is called, and it creates a non-const object, so I don't get it.

I'd have to dig through the standard to be sure, but your comments here have triggered a recollection of a discussion I saw by some gurus a couple of years back. IIRC, in your example,

Code:

m3 = m1 + m2;

the result of the addition (returned from operator+()) is allowed to be stored in a temporary, and that temporary is passed by reference to operator=()

Code:

Matrix temporary(m1+m2);
m3.operator=(temporary);

The problem is that, if the compiler does this, the temporary is an rvalue, so can only be bound to a const reference. Hence an error message --- even if the compiler is subsequently smart enough to optimise the temporary out of existance.