using四大用法

1.命名空间

using namespace 命名空间;//这样每次使用命名空间中的变量时就不用指定命名空间了

注意:头文件中不应有using命名空间的声明

2.类型别名声明(C++11)

using ll = long long;//等价于typedef long long ll
typedef double db, *p;//db是double的同义词,p是double*的同义词(注意)

3.改变从基类继承来的成员函数的访问性

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.让派生类对基类中所有的重载函数都可见,而不是隐藏

class base {
public:
    void func()
    {
        cout << "func()1" << endl;
    }
    void func(int x)
    {
        cout << "func()2" << endl;
    }
};

class son : public base {
public:
    using base::func;    //若没有此句,func()和func(int x)将会被隐藏
    void func(int x, int y)
    {
        cout << "func()3" << endl;
    }
};

猜你喜欢

转载自www.cnblogs.com/Joezzz/p/9927494.html