"Effective C + +" clause 33: Avoid concealing the inherited name

Source: Internet
Author: User

"Effective C + +"

Article 33: Avoid hiding the inherited name

Masking behavior is related to scope. Examples are as follows:

int x;//global variable void Somefun () {Double x;//local variable std::cin >> x;//read a new value give local variable x}

The statement that reads the data refers to the local variable x, not the global variable x, because the name of the inner scope obscures the name of the perimeter scope.

For example:

Class Base{private:int x;public:virtual void mf1 () = 0;    virtual void mf2 ();    void Mf3 (); ...};    Class derived:public base{public:virtual void Mf1 ();    void Mf4 (); ...};

This example contains a set of mixed public and private names, and a set of member variables and member function names. These member functions include pure virtual, impure virtual, and non-virtual three, which is to emphasize that we are talking about names, and nothing else.

Assume that the implementation code for the MF4 within the derived class is as follows:

void Derived::mf4 () {... mf2 (); ...}

When the compiler sees that MF2 is used here, it must estimate what it refers to. The compiler's approach is to find the scopes and see if there is a declaration named MF2. First find the local scope (that is, the scope of the MF4 overlay), where nothing is found named Mf2. Then look for other perimeter scopes, which are the scopes covered by class derived. Still haven't found anything called mf2, so move to the periphery, this example is base class. There the compiler found something named Mf2 and stopped looking. If there is no mf2 within the base, the find action goes on, first looking for the scope of the namespace that contains the base, and finally looking for the global scope.

class base{private:    int x;public:     VIRTUAL VOID MF1 ()  = 0;    virtual void  MF1 (int);     virtual void mf2 ();     void mf3 ();     VOID MF3 (Double);    ...}; CLASS DERIVED : PUBLIC BASE{PUBLIC:    VIRTUAL VOID MF1 ();     void mf3 ();     void mf4 ();      ...};D ERIVED D;INT X;...D.MF1 ();//okd.mf1 (x);//error!!!  DERIVED::MF1 conceals base::mf1d.md2 ();//okd.mf3 ();//okd.mf3 (x);//error!!!  derived::mf3 hides the base::mf3 

to achieve this goal:

class base{private:    int x;public:     VIRTUAL VOID MF1 ()  = 0;    virtual void  MF1 (int);     virtual void mf2 ();     void mf3 ();     VOID MF3 (Double);    ...}; class derived : public base{public:    using base::mf1;//Let Base Everything named Mf1 and mf3 in  class is visible in the derived scope     using Base::mf3;         VIRTUAL VOID MF1 ();     void mf3 ();     void mf4 ();     ..};D Erived d;int x;...d.mf1 ();//OKd.mf1 (x);// OK, call BASE::MF1D.MD2 ();//okd.mf3 ();//okd.mf3 (x);//ok, call Base::mf3 

That is, if you inherit base class and add overloaded functions, and you want to redefine or overwrite some of them, you must introduce a using declaration for each name that would otherwise be obscured, or the name you wish to inherit will be obscured.

Sometimes you don't want to inherit all the functions of base class, which is understandable. Under public inheritance, this is absolutely not possible. But the private form inherits base, and the only mf1 that derived wants to inherit is that parameterless version. The using declarative style is not useful here. A simple transfer function (forwarding functions)

Class base{public:virtual void mf1 () = 0;    virtual void mf1 (int); ...};    Class derived:private base{public:virtual void Mf1 ()//Transfer function secretly for inline {base::mf1 ();} ...};D erived d;int x;d.mf1 ();//ok, call DERIVED::MF1D.MF1 (x);//error! BASE::MF1 () is obscured

Summarize:

    1. The name within the derived classes will obscure the name within the base classes. No one has ever wished to do so under public inheritance.

    2. To let the masked name see the daylight, use a using declarative or a transfer function (forwarding functions).

2016-11-09 12:56:26

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1871037

"Effective C + +" clause 33: Avoid concealing the inherited name

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.