Why only the = operator is not inherited?

Started by
5 comments, last by noatom 10 years, 10 months ago

So,ALL the other operators are inherited,but only the = one is not.Can someone help with some explanations?

Advertisement

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.

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:

destructor

copy constructor

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.

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.

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.

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.

Well it's still better,if it would've used the base classe's operator,all it would do is update only the base class part of the entire object.

This topic is closed to new replies.

Advertisement