order of precedence

if I have:
Cars car1 = car2 + car3;

the + operator is overload to return a Cars object.
When this is call...what happens first ? It looks like my code is trying to create a car1 object first, because it errors with 'no matching function'.


Thanks

it errors with 'no matching function'

Your overloaded + function is probably not correct.
Can't tell for sure without seeing your class declaration.
it works like this: Cars car1; car1 = car2 + car3;
but not Cars car1 = car2 + car3;
http://www.cplusplus.com/doc/tutorial/operators/

On the very end of this site you have a table answering your question.
Cars car1 = car2 + car3;

Although it's using the '=' character, this is actually an initialisation, so it's the same as:

 
Cars car1(car2 + car3);


Assuming car2 and car3 are of type Cars, then the result of car2 + car3 should be of type Cars. This means that car1 is created using the copy constructor of Cars.

Have you written a copy constructor for Cars?
Last edited on
nmn wrote:
When this is call...what happens first ? It looks like my code is trying to create a car1 object first, because it errors with 'no matching function'.

The order the code is compiled doesn't necessary have anything to do with the order in which things happen at runtime.

MikeyBoy gave the correct explanation of why you get the error you do.
thanks all...great stuff.
How in the world do you prototype this copy constructor ?

Cars car1(car2 + car3);
There is only 1 way to prototype any copy constructor because there is only 1 copy constructor:

 
Cars( const Cars& car_to_copy );
OK... i think i see it. the (car2 + car3) gets evaluated first, then the reference of this result is used. Right ?
correct.
Topic archived. No new replies allowed.