are assignment operators only called in multiple assignment statements like:
or are there other times?Code:obj3 = obj2 = obj1;
Printable View
are assignment operators only called in multiple assignment statements like:
or are there other times?Code:obj3 = obj2 = obj1;
Assignment operators are called whenever you perform assignment.
hmm... I am not entirely sure how to interpret your statement. I'm inclined to say yes, there are other times, such as when operator chaining is not used:
But that sounds too obvious.Code:obj2 = obj1;
but if I write something like this into the assignment operator:
I don't see the message for single assingment statements like:Code:
if( &rhs != this ){
std::cout << "Preforming assignment.\n";
// normal assignment duties
}
return *this;
only in multiple assignments like:Code:obj2 = obj1;
Code:obj3 = obj2 = obj1;
Show more code, like a complete example.
Are you sure you're not doing this:If that's the case, then you are calling the copy constructor for obj2, not the copy assignment operator. You would want to do this instead:Code:Object obj1;
Object obj2 = obj1;
Code:Object obj1;
Object obj2;
obj2 = obj1;
It happens to be copy constructor syntax.Quote:
Why is the copy constructor called instead of the assignment operator in that example?
AndCode:Object obj1;
Object obj2 = obj1;
Are both copy constructor. When initializing an object, the copy constructor is called. This is regardless if you use () or =.Code:Object obj1;
Object obj2(obj1);
>> Why is the copy constructor called instead of the assignment operator in that example?
Because you're constructing obj2.
Built-in types are definied and initialized with that syntax, so the syntax was used for classes as well. For built-in types, there really isn't such a thing as construction, so assignment at initialization and assignment afterwards wasn't much different and shared the same syntax.
Note that there are still some differences between the two syntaxes, although I forget what they are at the moment.
If the copy constructor is declared explicit, only the:Quote:
Note that there are still some differences between the two syntaxes, although I forget what they are at the moment.
form can be used.Code:Object obj2(obj1);
I think the difference is when the right side is not an object of the same type.
With the explicit constructor call syntax, the appropriate constructor is called.
With the assignment initialization syntax, a temporary is created using the appropriate constructor, and then the actual object is initialized using the copy constructor.
But I'm not 100% sure about this. To test, make the copy constructor private and see if both forms work.
Edit: laserlight probably knows better than me.
Yes, I think that is the case, but the compiler is allowed to elide the otherwise extra constructor call.Quote:
With the assignment initialization syntax, a temporary is created using the appropriate constructor, and then the actual object is initialized using the copy constructor.
But only if it is side-effect-free, I think. Otherwise, no compiler would ever create the temporary. It's not like RVO or NRVO, where eliding the copy actually takes some implementation effort.