So,ALL the other operators are inherited,but only the = one is not.Can someone help with some explanations?
Why only the = operator is not inherited?
Moderators - Reputation: 10141
Posted 18 June 2013 - 04:44 PM
It is inherited. However, unlike other operators, a derived class will generate an implicitly defined assignment operator that will hide the base class version. However, with explicit qualification the derived class can access the base class version if it wants to.
Moderators - Reputation: 27971
Posted 18 June 2013 - 05:02 PM
It works that way because the language standard says so. Specifically:
(13.5.3 Assignment) An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a a class if not declared by the user, a base class assignment operator is always hidden by the copy assignment operator of the derived class.
One thing about C++ is that the language assumes the the programmer is always right. It assumes the programmer knows the standard completely and is abiding by it. If the standard says something and the programmer does something different, the compiler will blindly accept the programmer's input even when it is a bug.
It used to be called the Rule of Three, but now with C++11 it is the rule of five. If you implement any one of these, you should implement all of them:
move constructor (C++11)
copy assignment operator
move assignment operator (C++11)
If your base class implements them, you should implement them as well. This is just one of the many
sources of bugs fun features of the C++ language.
Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.
Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.
Moderators - Reputation: 10141
Posted 18 June 2013 - 07:19 PM
I wouldn't go so far as to say that a base class implementing the copy/move operations or the destructor is a strong indication that a derived class should. The compiler generated defaults will call the base class versions. If your base class requires derived classes to do anything more than that then that's a pretty good indication that the base class versions are implemented incorrectly.
Members - Reputation: 789
Posted 19 June 2013 - 03:03 AM
Actually,it makes perfect sense to synthesize the = operator and not inherit it.I mean think about it: the synthesized one will just perform bit copy and call the = operator of the base class.
If it did keep the inherited one,then it would perform the required operations only on the base class part of the object.
This way,even though it's rudimentary,it performs basic bit copy for the current object too,while it also call the = operator for the base class.
Edited by noatom, 19 June 2013 - 03:04 AM.
Moderators - Reputation: 9240
Posted 19 June 2013 - 04:22 AM
The generated operator = does not make a bit copy, but calls operator = for all its members. Those operator ='s may or may not do a bit copy, but don't count on it. The assignment operator for, say, std::string certainly doesn't just copy the bits since it has to manage and allocate the memory for the internal string data.