Function of using in C ++

Source: Internet
Author: User

1. Introduce the namespace in the current file

This is our most familiar usage, for example: Using namespace STD;

2. Use the using declaration in the subclass to introduce the base class member name (see C ++ primer)

When private or protected is inherited, the access level of the base class members is more restricted in the derived class:

 
ClassBase {Public: STD: size_t size ()Const{ReturnN ;}Protected: STD: size_t N ;};ClassDerived:PrivateBase {...};

In this inheritance level, the member function size is public in base, but private in derived. To make the size public in derived, you can
Add a using declaration. The definition of derived is changed as follows, so that the size member can be accessed by the user and the N can be accessed by the derived class of derived:

 
ClassDerived:PrivateBase {Public:UsingBase: size;Protected:UsingBase: N;//...};

In addition, when the member functions in the subclass have the same name as the base class, the member functions defined in the subclass hide the versions in the base class, even if the function prototypes are different. If the base class member function has multiple overloaded versions, the derived class can redefine the inherited 0 or multiple versions, but only the versions that are redefined in the derived class can be accessed through the derived type, therefore, if a derived class wants to use all the overloaded versions of its own type, the derived class must either redefine all the overloaded versions or one of them. Sometimes classes only need to redefine the behavior of some versions in a reload set and want to inherit the meaning of other versions. In this case, it may be annoying to have to redefine each base class version to redefine a specific version. You can provide the names of the overloaded members in the derived class.
Using Declaration (the using Declaration for the base class member function name adds all the overloaded instances of the function to the scope of the derived class), so that the derived class does not need to redefine each inherited base class version. A using Declaration can only specify one name, but cannot specify the form parameter table. After the using declaration adds the name to the scope, the derived class only needs to redefine the functions that must be defined for this type, you can use an inherited definition for other versions.

Below isCodeExample:

# Include <iostream> Using   Namespace STD; Class Base { Public : Int Menfcn () {cout < "  Base Function  " <Endl; Return   0 ;}}; Class Derived: Base { Public : Using Base: menfcn; //  The Using Declaration can only specify one name, but cannot contain a form parameter table.        Int Menfcn ( Int );}; Int Main () {Base B; derived D; B. menfcn (); D. menfcn (); //  If the using declaration in the derived class is removed, the following error occurs: Error c2660: 'derived: menfcn ': function does not take 0 arguments.   STD: cin. Ignore (STD: cin. gcount () + 1 ); // Clear the buffer   STD: cin.Get (); // Pause Program Execution}

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.