C + + using keyword Action Summary

Source: Internet
Author: User
Tags function prototype

1. Introducing namespaces in the current file

This is our most familiar usage, for example:using namespace std;

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

when private or protected inherits, the access level of a base class member is more limited in a derived class:

Class Base {
Public:
std::size_t size () const {return N;}
Protected:
std::size_t N;
};
Class Derived: private Base {...};

In this inheritance hierarchy, member functionsSizeInBaseIn thePublic, but in Derived Private In order to make Size Derived Public can be Derived public
part adds a using Span style= "color:black;" > declaration. The following changes are made to Derived size Span style= "color:black;" > members can be accessed by users and make N derived< Span style= "color:black;" -derived class access:

Class Derived: private Base {
Public:
Using base::size;
Protected:
Using base::n;
// ...
};

Also, when a member function in a subclass has the same name as a base class, the redefined member function in the subclass hides the version in the base class, even if the function prototype is different (hide condition see below).

If a member function in a base class has more than one overloaded version, the derived class can redefine the inherited 0 or more versions, but only those versions that are redefined in the derived class are accessible through derived types , so if the derived class wants to use all of the overloaded versions by its own type, the derived class must either redefine all overloaded versions , or one is not redefined. Sometimes a class needs to simply redefine the behavior of some versions of an overload set and want to inherit the meaning of other versions, in which case it can be tedious to redefine each base class version to redefine a version that needs to be specific. You can provide a using declaration for an overloaded member name in a derived class (a using declaration for a base class member function name adds all overloaded instances of the function to the scope of the derived class), so that derived classes do not have to redefine each version of the base class that is inherited. A using declaration can only specify a name, cannot specify a formal parameter list, and after the name is added to the scope with a using declaration, the derived class simply needs to redefine those functions that the type does have to define, and can use the inherited definition for other versions.

"Hidden" refers to a function of a derived class that masks a base class function with the same name as the following rule:

1. If the function of the derived class has the same name as the function of the base class, the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (note not to be confused with overloading)

2. If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)

#include "StdAfx.h"
#include <iostream>
using namespace Std;

Class Base
{
Public
void Menfcn ()
{
cout<< "Base function" <<endl;
}

void MENFCN (int n)
{
cout<< cout<< "Base function with int" <<endl;
}

};

Class Derived:base
{
Public
Using base::menfcn;//using declaration can only specify a name, cannot take formal parameter list
int MENFCN (int)
{cout<< cout<< "Derived function with int" <<endl;}
};
int main ()
{Base B;
Derived D;
B.MENFCN ();
D.MENFCN ();//If you remove the using declaration from the Derived class, an error occurs: "Error C2660: ' DERIVED::MENFCN ': function does not take 0 arguments std::cin.i Gnore (Std::cin.gcount () +1);//emptying buffer std::cin.get ();//Pause program execution
}

C + + using keyword Action Summary

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.