Home > Article > Backend Development > What is the usage of using
using usage is: 1. Namespace [using namespace namespace]; 2. Type alias [using aa=double]; 3. Change the access level of members inherited from the base class; 4. Let the derived The class is visible to all overloaded functions in the base class; 5. Let the derived class inherit all constructors.
【Related learning recommendations: C language tutorial video】
using usage is:
1. Namespace
using namespace 命名空间;//这样每次使用命名空间中的变量时就不用指定命名空间了
Note: There should be no using in the header file Namespace declaration
2. Type alias (C 11)
using aa = double;//等价于typedef double aa typedef double db, *p;//db是double的同义词,p是double*的同义词(注意)
3. Change the access level of members inherited from the base class
class base { public: int fun(int x); int b; }; class son : private base { public: using base::fun; //fun(int x)由private变成public(注意:using不指定参数列表) protected: using base::b; //b由public变成protected };
4. Make the derived class visible to all overloaded functions in the base class instead of hiding it
class base { public: void func() { cout << "func1()" << endl; } void func(int x) { cout << "func2()" << endl; } }; class son : public base { public: using base::func; //若没有此句,func()和func(int x)将会被隐藏 void func(int x, int y) { cout << "func()3" << endl; } };
5. Upgrade of point 4 , using allows the derived class to inherit all the constructors of the direct base class
1) The derived class does not inherit the constructor of the base class in the conventional way, but uses the using statement to let the derived class inherit the base class All (with two exceptions) constructors
2) Exception 1: The derived class’s own constructor has the same formal parameters as a constructor of the base class, then the constructor will not be inherited
Exception 2: Default, copy and move constructors will not be inherited
3) Unlike point 3, the using statement will not change the access level of the inherited constructor: regardless of the using statement Where it appears, the public/protected/private constructor of the base class is still a public/protected/private constructor in the derived class.
4) The using statement cannot specify explicit and constexpr: if the base class If the constructor is explicit or constexpr, the inherited constructor also has the same attributes
5) The inherited constructor is not a custom constructor, so it can still meet the generation rules of synthetic constructors
6) If the constructor of the base class has default arguments, the actual default parameters will not be inherited
7) If the constructor of the base class has two formal parameters, one formal parameter does not have a default argument parameter, and the other formal parameter has default actual parameters, then two constructors will be obtained through inheritance: one constructor has two formal parameters, and the other has only one formal parameter (corresponding to the one without default actual parameters). In short, the default actual parameters None of the parameters will be inherited
class base { public: base() {} base(string s, int a) {} }; class son : public base { public: using base::base;//继承直接基类所有的构造函数,对于基类的每个构造函数,编译器都在派生类中生成一个形参完全相同的构造函数 //using不能指定explicit和constexpr private: //使用继承来的构造函数时,如果派生类有自己的数据成员,则这些成员将被默认初始化 string str; int num; }; //等价于: class son : public base { public: son():base() {} son(string s, int a): base(s, a) {} };
The above is the detailed content of What is the usage of using. For more information, please follow other related articles on the PHP Chinese website!