Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why only the = operator is not inherited?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 noatom   Members   -  Reputation: 781

Like
0Likes
Like

Posted 18 June 2013 - 04:15 PM

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



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9489

Like
4Likes
Like

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.



#3 frob   Moderators   -  Reputation: 19606

Like
3Likes
Like

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:

 

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.


Check out my personal indie blog at bryanwagstaff.com.

#4 SiCrane   Moderators   -  Reputation: 9489

Like
2Likes
Like

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.



#5 noatom   Members   -  Reputation: 781

Like
0Likes
Like

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.


#6 Brother Bob   Moderators   -  Reputation: 7863

Like
0Likes
Like

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.



#7 noatom   Members   -  Reputation: 781

Like
0Likes
Like

Posted 19 June 2013 - 05:38 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS