[Turn] C ++ using

Transfer from https://blog.csdn.net/luoshabugui/article/details/86632421


Before using a C ++ 11

1 using affirm

"C ++ primer plus" 中:

using stated: using + qualified name

Defining Name: contains the name of the namespace

For example:

using std::cout;
  
  

2 using compiler directives

For example:

using namespace std;
  
  

using compiler directives can pass.

The use of two C ++ 11

1 replace typedef

For example:

using intvec = std::vector<int>;
  
  

2 C ++ 11 added in succession constructors , in which a using

3 member functions hidden usage scenarios

Examples 2 and 3:


   
   
  1. #include <iostream>
  2. #include <vector>
  3. class A {
  4. public:
  5. A() :m_( 0) {}
  6. A( int n) :m_(n) {}
  7. virtual void show() {
  8. std:: cout << "A show" << std:: endl;
  9. }
  10. virtual void show(int n) {
  11. std:: cout << "A show int " << n << std:: endl;
  12. }
  13. void test() {
  14. std:: cout << "A test" << std:: endl;
  15. }
  16. void test(int n) {
  17. std:: cout << "A show test " << n << std:: endl;
  18. }
  19. int m_;
  20. };
  21. class AA : public A {
  22. public:
  23. using A::A; // 场景2
  24. using A::show; // 场景3
  25. using A::test; // 场景3
  26. virtual void show() {
  27. std:: cout << "AA show" << std:: endl;
  28. }
  29. void test() {
  30. std:: cout << "AA test" << std:: endl;
  31. }
  32. };
  33. int main() {
  34. AA aa;
  35. aa.show();
  36. aa.test();
  37. aa.show( 1);
  38. aa.test( 1);
  39. return 0;
  40. }

result:

"C ++ primer plus" 中:

If the function is redefined in a derived class will not only use the same function parameter list covers the base class declaration, regardless of whether the same list of parameters, which will hide all the base class method with the same name have.
Two lead-out rule of thumb:
First, redefine an inherited method if, should ensure that the original prototype identical, but if the return type is a base class reference or pointer, may be modified to a reference or pointer to the derived class, such feature is called covariant return types (covariance of return type), as to allow the return type varies depending on the type of change;
second, if the base class is overloaded stated, all should be re-defined in the base class version of the derived class .

Popular speak

Note i.e. hidden scenario:
If the function is a function of the base class derived from the same name, but different parameters. At this point, whether or not the virtual keyword, the base class will be hidden.
If the function of the same name and function of the derived class's base class, and the parameters are the same, but not the base class virtual function key. At this time, the base class is hidden.

 So the above code, the code commented out two scenarios 3, AA and show function test can not be called with parameters.

4 alias template

For example:


   
   
  1. // 别名模板
  2. template< class T>
  3. using ptr = T*;
  4. // 名称 'ptr<T>' 现在是指向 T 指针的别名
  5. ptr< int> x;

Three reference

using

Guess you like

Origin www.cnblogs.com/lzping/p/12186673.html