1. 使用命名空间 这是using最常见的用法,或者最开始知道的用法,至少对于我来说是这样。接触的第一个C++程序会带有这么一句话:
至于命名空间以及为什么使用命名空间在此不再赘述。
2. 使用别名 今天刚了解到的一种用法,类似typedef,可以给现有的类型重定义一个别名,比如:
1 using uint = unsigned int ;
其与使用typedef重定义等价:
1 typedef unsigned int uint;
从上面可以看,貌似using和typedef没什么区别,实际上在重定义普通类型的时候,二者的效果是等价的,但是以下两种用法却有了一定的区别。
2.1 函数指针的别名 使用typedef重定义一个函数指针:
1 typedef int (*func_ptr) (int , int ) ;
如果不是特别熟悉typedef与函数指针,很难一眼看出func_ptr
是个别名,其本质是个函数指针。但是如果使用using重定义一个函数指针:
1 using func_ptr = int (*)(int , int );
using强制把别名的名字分离到了左边,把别名对应的实际类型放在了右边,更加直观。
2.2 模板的别名 typedef无法重定义模板,比如我们需要一个固定以int
类型为key的map,它可以和很多类型的value值进行映射,如果使用typedef这样定义就非常麻烦。
1 2 3 typedef map<int , string> mapS;typedef map<int , int > mapI;typedef map<int , double > mapF;
自然而然的我们就想到了模板:
1 2 template <typename T>typedef map<int , T> mapT;
可惜,typedef不支持给模板定义别名,如果真的想用typedef定义,需要写成这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> #include "test1.cpp" #include "map" using namespace std;template <typename T>struct int_map { typedef map<int , T> mapT; };int main () { int_map<int >::mapT m1; m1[1 ] = 1 ; int_map<double >::mapT m2; m2[1 ] = 0.1 ; }
如果使用using,很容易对模板进行重定义:
1 2 template <typename T>using mapT = map<int , T>;
3. 改变派生类继承权限 派生类私有继承基类时,基类的public和protected数据成员在派生类中是private的形式,如果想让这些继承而来的数据成员作为public或者protected成员,可以用using来重新声明。using声明语句中名字的访问权限由该using声明语句之前的访问说明符决定。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include "iostream" using namespace std;class Base {protected : int value;public : void fun1 () { cout << "I'm Base" << endl; } };class Derived : private Base {public : };int main () { Derived derived; derived.fun1 (); cout << derived.value << endl; }
使用using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include "iostream" using namespace std;class Base {protected : int value;public : void fun1 () { cout << "I'm Base" << endl; } };class Derived : private Base {public : using Base::value; using Base::fun1; };int main () { Derived derived; derived.fun1 (); cout << derived.value << endl; }
4. 派生类函数重载 如果基类中成员函数有多个重载版本,派生类可以重定义所继承的基类成员函数,但是派生类对象只能访问派生类中重定义的那些版本 。所以如果派生类想通过自身类型使用所有的重载版本,则派生类必须要么重定义所有重载版本 ,要么一个也不重定义 。
有时类需要仅仅重定义一个重载集中某些版本的行为,并且想要继承其他版本的含义,在这种情况下,为了重定义需要特化的某个版本而不得不重定义每一个基类版本,属实有点恶心。此时可以在派生类中为重载成员函数名称使用 using 声明(为基类成员函数名称而作的 using 声明将该函数的所有重载实例加到派生类的作用域),使派生类不用重定义所继承的每一个基类版本。
一个 using 声明只能指定一个名字,不能指定形参表,使用using声明将名字加入作用域之后,派生类只需要重定义本类型确实必须定义的那些函数,对其他版本可以使用继承的定义。
不使用using声明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include "iostream" using namespace std;class Base {public : void fun () { cout << "I'm Base" << endl; } void fun (int a) { cout << a << endl; } void fun (char a) { cout << a << endl; } };class Derived : private Base {public : void fun () { cout << "I'm Derived" << endl; } };int main () { Derived derived; derived.fun (); derived.fun (1 ); }
使用using声明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include "iostream" using namespace std;class Base {public : void fun () { cout << "I'm Base" << endl; } void fun (int a) { cout << a << endl; } void fun (char a) { cout << a << endl; } };class Derived : private Base {public : void fun () { cout << "I'm Derived" << endl; } using Base::fun; };int main () { Derived derived; derived.fun (); derived.fun (1 ); }