using关键字在日常使用中会应用到的场景
引用namespace
在此文件中声明了std这个标准命名空间,本文件或者包含本文件的文件就可以直接使用std命名空间中的函数、类、变量等。
using namespace std;
将基类的成员可访问
#include <iostream>
class Base
{
public:
void test() {std::cout<<m_head<<std::endl;}
protected:
int m_head;
};
class Derive : private Base
{
public:
//using Base::m_head;
using Base::test;
void test2(){m_head++;}
void show(){std::cout<<m_head<<std::endl;}
};
int main(int argc, char const *argv[])
{
Derive d;
d.show();
d.test2();
d.show();
/*这里可以访问到*/
d.test();
return 0;
}
别名
c++11标准以后才能使用
typedef void(*func)(int ,int );
/* 等价于 */
using func=void(*)(int,int);
#include <vector>
using namespace std;
template<typename T>
using Vec = vector<T,allocator<T>>;
Vec<int> vec;