C++ using的用法

转载:https://blog.csdn.net/weixin_39640298/article/details/84641726

学习使用C++也有六七年了,感觉一直学的是C++98的基础版本语法,最近在看一些优秀的开源库,里面使用的都是新特性,想借鉴用到自己的项目中,发现有些新特性在低版本的VS中不支持,因此要转换成基础版的语法,在此记录一下using的特殊用法。

1.印象中用到using都是在命名空间上 ,例如:using namespace std; using xxxx等等

using std::cin;    //using声明,当我们使用cin时,从命名空间std中获取它

2.命令空间的using声明

class Base 
{
protected:
    void test1() { cout << "test1" << endl; }
    void test1(int a) {cout << "test2" << endl; }

    int value = 55;
};
 
class Derived : Base     //使用默认继承
{
public:
    //using Base::test1;    //using只是声明,不参与形参的指定
    //using Base::value;
    void test2() { cout << "value is " << value << endl; }
};

3.使用using起别名

相当于传统的typedef起别名

typedef     std::vector<int> intvec;
using     intvec = std::vector<int>;    //这两个写法是等价的
原文地址:https://www.cnblogs.com/chechen/p/14720200.html