C + + Inheritance: Hidden with the same name

Source: Internet
Author: User

The visibility principle for identifiers declared in different scopes:

If there are two or more scopes with a containing relationship, the outer layer declares an identifier, and the inner level does not declare the same identifier again, the outer identifier is still visible in the inner layer, and if an identifier of the same name is declared in the inner layer, the outer identifier is invisible at the inner layer, and the inner identifier hides the outer identifier of the same name This behavior is known as a hidden rule.

In a derived hierarchy of classes, the members of the base class and the new members of the derived class have class scopes. The two are different in scope, and are two layers of each other, derived classes in the inner layer. At this point, if a derived class declares a new member with the same name as a base class member, the derived new member hides the member with the same name, directly using the member name to access only the members of the derived class.

If a new function with the same name as the base class is declared in a derived class, all overloaded forms of the function with the same name inherited from the base class are hidden, even if the function's parameter table is different. If you want to access a member that is hidden, you need to qualify it with the class scope and base class names.

The scope resolution is "::", which can be used to qualify the name of the class to which the member is to be accessed. The general form of use is:

Class Name:: member name

Class Name: member name (parameter table)

 1#include <iostream>2 using namespacestd;3  4 classA5 { 6      Public: 7         voidPrint2 () {cout <<"A print2!"<<Endl;} 8 }; 9 Ten classD | PublicA One { A      Public: -         voidPrint2 (intx) -         { thecout <<"B Print2!"<< x <<Endl; -         } - }; -  + intMain () - { +b b; AB.print2 (); at     return 0; -}

Compilation failed:

[[email protected] test] $g ++? NT Main ()? . A1.cxx: : Error:no matc hing function for call to?.::p Rint2 ()? a1.cxx:13:note:candidates are:void B::p rint2 (int )

As the result shows, it is not possible to access Print2 () directly from the object of B with the function name.

Change B.print2 () to B. A::p rint2 ();

Compile the pass and execute the result as follows:

[Email protected] Test]$./a.out a print2!

C + + Inheritance: Hidden with the same 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.