Effective C + + 33 Avoid masking inherited names

Source: Internet
Author: User

First introduce a principle LSP (Liskov Substitution Principle), if Class D inherits Class B in public, then all B objects can be used anywhere, D objects are just as useful.

For overloading and rewriting, I'm sure we all know something about it. Here we discuss the problem of overloaded functions when the public inherits.

Let's look at the following example:

1 classBase2     {  3      Public:  4         Virtual voidMF1 () =0; 5         Virtual voidMF1 (int); 6         Virtual voidMf2 (); 7         voidmf3 (); 8         voidMF3 (Double); 9         ...  Ten     Private:   One         intX_;  A     };  -     classDerived: PublicBase -     {   the      Public:   -         Virtual voidmf1 ();  -         voidmf3 ();  -         voidMf4 ();  +         ...   -};

In the base class, MF1 () and MF3 () are overloaded, and the subclass public inherits from the parent class, where the subclass overrides all functions named Mf1 and MF3 in the parent class. Thus some of the following calls will fail.

Derived D;   int x;  ...   // derived::mf1   // error derived::mf1 Hiden base::mf1   D.mf2 ();  // base::mf2   D.mf3 ();  // derived::mf3   // error derived::mf3 Hiden base::mf3

Thus, the function of a subclass can overwrite a function with the same name in all the parent classes when there are different arguments to the function in the child class and the parent class.

The reason for this: avoid inheriting overloaded functions from the estranged parent class. However, in fact, public inheritance should follow the LSP principle that the parent class and subclass are is-a relationships, so we can implement the inheritance of overloaded functions with the same name as the parent class by using a display.

1 classBase{...};//Ibid .2 classDerived: Publicbase{3  Public:  4     //so that everything in the base class named Mf1 and Mf35      //visible (and public) within the derived scope6     usingBase::mf1; 7     usingbase::mf3; 8....//Ibid .9};
Derived D;   int x;  ...   // no problem. Call Derived::mf1   // no problem. BASE::MF1  d.mf2 ();  // no problem. Call Base::mf2   D.mf3 ();  // no problem. Call Derived::mf3   // no problem. BASE::MF3  

If you do not want to inherit all the overloaded functions, then you should not use public inheritance, because this violates the LSP principle, you can use private inheritance, in order to selectively inherit overloaded functions, you can use the inline transfer function (forwarding functions), The specific implementation is as follows:

1 classBase2 {  3  Public:  4     Virtual voidMF1 () =0; 5     Virtual voidMF1 (int); 6...//Same as above7 }; 8 classDerived:PrivateBase9 {  Ten  Public:   One     Virtual voidMF1 ()//Transfer functions (forwarding function) A     {   - base::mf1 ();  -     }   the     ...   - };  - ...   - Derived D;  + intx;  -D.MF1 ();//good! Call Derived::mf1 +D.MF1 (x);//Error as SUPPOSE,BASE::MF1 () was hidden. 

The essential:

Names within subclasses obscure names within the parent class, which should not be done under PUBILC inheritance.

To make the masked name visible, use a using declaration or a transfer function.

Effective C + + 33 Avoid masking inherited names

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.