Summary of the functions of the C ++ Using Keyword

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 Using in the subclass
Declare 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:

Class base {
Public:
STD: size_t size () const {
Return N ;}
Protected:
STD: size_t N;
};
Class derived:
Private base {...};

In this inheritance level, the member function size
Public in base, but in derived
Private. To make the size
In derived
Public, which can be in derived
Public
Add a using
Statement. Change derived as follows
To enable the size member to be accessed by the user and enable n
It can be accessed by derived's derived class:

Class derived:
Private base {
Public:
Using Base: size;
Protected:
Using Base: N;
//...
};

In addition, when a member function in a subclass has the same name as the base class, the member function defined in the subclass hides the version in the base class, even if the function prototype is different (see the following hidden conditions ).

If the base class member function has multiple overloaded versions, the derived class can redefine the inherited 0
Versions,Only versions defined in the derived class can be accessed.So if the derived class wants to use all the overloaded versions of its own type, the derived class mustYou can either redefine all overloaded versions.,Either one is not redefined. 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.

"Hide" means that the function of the derived class shields the base class functions with the same name. The rules are as follows:

1. If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden no matter whether there is any virtual keyword. (Be sure not to confuse them with overload)

2. If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Class base
{
Public:
Void menfcn ()
{
Cout <"base function" <Endl;
}

Void menfcn (int n)
{
Cout <"base function with int" <Endl;
}

};

Class derived: Base
{
Public:
Using Base: menfcn; // The using Declaration can only specify one name, and does not contain a form parameter table.
Int menfcn (INT)
{Cout <"derived function with int" <Endl ;}
};
Int main ()
{Base B;
Derived D;
B. menfcn ();
D. menfcn (); // If the using declaration in the derived class is removed, an 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.