Sign in to follow this  
noatom

Why only the = operator is not inherited?

Recommended Posts

noatom    927

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

Share this post


Link to post
Share on other sites
SiCrane    11839

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.

Share this post


Link to post
Share on other sites
frob    44919

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.

Share this post


Link to post
Share on other sites
SiCrane    11839

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.

Share this post


Link to post
Share on other sites
noatom    927

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

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
noatom    927

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this