C++之using用法

using

using是 C++ 中的一个关键字,可以分为两种用法,一种是声明,另一种是指示。

声明

  • 声明变量

    using 类型别名=原类型;
    
    using myint = int;
    myint a = 1; 		//等价于int a = 1
    

指示

  • 命名空间

    using namespace name;
    

    使用该用法能直接在程序中使用 using后所跟的命名空间的元素,而不用每次要使用时指定命名空间。

    using namespace std;
    //使用以上方式就可以直接使用std元素,如cout或cin而不是std::cout
    
  • 指示成员

    namespace T{
        void print(){
            cout<<"hello";
        }
    }
    using T::print();		//在接下来的代码中,可直接调用print()
    

    此方法支持重载和继承。

猜你喜欢

转载自blog.csdn.net/weixin_43675051/article/details/85158894
今日推荐