are assignment operators only called in multiple assignment statements like:
or are there other times?Code:obj3 = obj2 = obj1;
are assignment operators only called in multiple assignment statements like:
or are there other times?Code:obj3 = obj2 = obj1;
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
Assignment operators are called whenever you perform assignment.
My best code is written with the delete key.
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;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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;
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
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.Why is the copy constructor called instead of the assignment operator in that example?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> 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: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);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Yes, I think that is the case, but the compiler is allowed to elide the otherwise extra constructor call.With the assignment initialization syntax, a temporary is created using the appropriate constructor, and then the actual object is initialized using the copy constructor.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law