C++类型转换操作符(type conversion operator)

类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换。转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的目标类型。boost::ref和boost::cref就使用到了类型转换操作符。

 

函数原型 

  
    
T1:: operator T2() [ const] ;    // T1的成员函数,"(T2)a"类型转换

1. 转换函数必须是成员函数,不能指定返回类型,并且形参表必须为空;返回值是隐含的,返回值是与转换的类型相同的,即为上面原型中的T2;

2. T2表示内置类型名(built-in type)、类类型名(class type)或由类型别名(typedef)定义的名字;对任何可作为函数返回类型的类型(除了 void 之外)都可以定义转换函数,一般而言,不允许转换为数组或函数类型,转换为指针类型(数据和函数指针)以及引用类型是可以的;

3. 转换函数一般不应该改变被转换的对象,因此转换操作符通常应定义为 const 成员;

4. 支持继承,可以为虚函数;

5. 只要存在转换,编译器将在可以使用内置转换的地方自动调用它;

 

 

先通过一个简单的例子来说明如何使用类型转换操作符

  
    
1 #include < iostream >
2
3   class D {
4   public :
5 D( double d) : d_(d) {}
6
7 /* “(int)D”类型转换 */
8 operator int () const {
9 std::cout << " (int)d called! " << std::endl;
10 return static_cast < int > (d_);
11 }
12
13   private :
14 double d_;
15 };
16
17   int add( int a, int b) {
18 return a + b;
19 }
20
21   int main() {
22 D d1 = 1.1 ;
23 D d2 = 2.2 ;
24 std::cout << add(d1, d2) << std::endl;
25
26 return 0 ;
27 }
28
29  

在24行执行add(d1,d2)函数时“(int)D”类型转换函数将被自动调用,程序运行的输出为:

(int)d called!
(int)d called!
3

 

类型转换操作符 vs 类型转换构造函数(conversion constructor)

有时候使用conversion constructor就能实现类型转换,这种方式效率更高而且也更直观,下面举例说明:

  
    
1 #include < iostream >
2
3   class A
4 {
5   public :
6 A( int num = 0 ) : dat(num) {}
7
8 /* "(int)a"类型转换 */
9 operator int () { return dat; }
10
11   private :
12 int dat;
13 };
14
15
16   class X
17 {
18   public :
19 X( int num = 0 ) : dat(num) {}
20
21 /* "(int)a"类型转换 */
22 operator int () { return dat; }
23
24 /* "(A)a"类型转换 */
25 operator A() {
26 A temp = dat;
27 return temp;
28 }
29
30   private :
31 int dat;
32 };
33
34
35 int main()
36 {
37 X stuff = 37 ;
38 A more = 0 ;
39 int hold;
40
41 hold = stuff; // convert X::stuff to int
42   std::cout << hold << std::endl;
43
44 more = stuff; // convert X::stuff to A::more
45 std::cout << more << std::endl; // convert A::more to int
46
47 return 0 ;
48 }
49

上面这个程序中X类通过“operator A()”类型转换来实现将X类型对象转换成A类型,这种方式需要先创建一个临时A对象再用它去赋值目标对象;更好的方式是为A类增加一个构造函数:

  
    
A( const X & rhs) : dat(rhs) {}

同时,请注意上面程序的第45行more的类型在调用std::cout时被隐式地转成了int!

 

一个简单boost::ref实现

通过重载type conversion operator,我们就可以自己实现一个简版的boost::ref。

  
    
1 #include < iostream >
2
3 template < class T >
4   class RefHolder
5 {
6   public :
7 RefHolder(T & ref ) : ref_( ref ) {}
8
9 /* “(T&)A”类型转换操作符 */
10 operator T & () const {
11 return ref_;
12 }
13
14   private :
15 T & ref_;
16 };
17
18
19 template < class T >
20 inline RefHolder < T > ByRef(T & t) {
21 return RefHolder < T > (t);
22 }
23
24   int inc( int & num) {
25 num ++ ;
26 return num;
27 }
28
29
30   int main() {
31 int n = 1 ;
32 std::cout << inc(ByRef(n)) << std::endl; // RefHolder<int>被转换成了"int&"类型
33  
34 return 0 ;
35 }
36  

 

 

参考文章:

1. Wiki - Operators in C and C++

2. Cast operator overload

3. Overloading the typecast operator

4. Generic<Programming>: Change the Way You Write Exception-Safe Code Forever

5. C++ Primer - 14.9 Conversions and Class Types

你可能感兴趣的:(conversion)