C++的类型转换

数据类型是对内存的抽象,在实际的开发过程中,我们常常会遇到把一种类型转换成另外一种类型的情况。

那么,在C/C++中,类型转换都有哪些玩法呢?

C 的类型转换

写法:(type_name) expression

#include <stdio.h>
main() {
    double d = (double) 5;
    printf("%f\n", d);
    int i = (int) 5.4; // OK
    printf("%d\n", i);
    int i2 = (int) (5.4); // OK
    printf("%d\n", i2);
    int i3 = int (5.4); // Error
    printf("%d\n", i3);
    double result = (double) 4 / 5; // OK
    printf("%f\n", result);
}

C++的类型转换

把一种数据类型转换成另外一种数据类型,可以是显式的,也可以是隐式的。

C++中的隐式转换

隐式转换不需要任何转换运算符。

他们通常自动发生在将一个类型的值拷贝给另外一个兼容的类型时。

我们来看一个例子:

#include <iostream>
class A {
    int x = 0;
public:
    A(int x) { this->x = x; }
    int getX() { return x; }
};
class B {
    int x = 0, y = 0;
public:
    B(A a) { x = a.getX(); y = a.getX(); }
    void getXY(int &x, int &y) { x = this->x; y = this->y; }
};
int main() {
    A a(10);
    B b = a; // 隐式转换
    int x, y;
    b.getXY(x, y);
    std::cout << "x: " << x << " y: " << y << std::endl;
    return 0;
}

B b = a;处发生了隐式转换,程序输出是

x: 10 y: 10

C++ 关键词 explicit

C++11之后引入了关键词explicit,如果加了explicit, 将不允许 隐式转换复制初始化.

我们如果在上面的例子中加入explicit

explicit B(A a) { x = a.getX(); y = a.getX(); }

将会发生编译错误:

error: conversion from 'A' to non-scalar type 'B' requested

C++中的显示转换

我们可以像C一样的方式进行强制转换

B b = (B) a;
B b = B (a); 

像上面一样直接强转而不使用转换运算符,有啥坏处呢?

它可能导致代码编译通过运行出错。就像下面的例子:

#include <iostream>
class Data { float i = 5, j = 6; };
class Addition {
   int x, y;
public:
   Addition(int x, int y) { this->x = x; this->y = y; }
   int result() { return x + y; }
};
int main() {
    Data d;
    Addition *add = (Addition *) &d;
    std::cout << add->result();
    return 0;
}

几种转换运算符

传统的显式类型转换允许将任何指针转换为任何其他指针类型,而与它们指向的类型无关。这可能导致运行错误或其他意外的结果。

为了更好的控制类型转换,C++提供了四种基础的转换运算符:

static_cast, const_cast, dynamic_cast, reinterpret_cast.

static_cast

static_cast通常用于转换非多态类型

static_cast转换运算符可以用来执行任何隐式转换,包括标准类型和用户定义的类型。就像下面这样:

typedef unsigned char BYTE;  
void f() {  
   int i = 65;  
   float f = 2.5;  
   char ch = static_cast<char>(i);   // int to char  
   double dbl = static_cast<double>(f);   // float to double  
   i = static_cast<BYTE>(ch);  
} 

static_cast转换运算符可以将一个整型数据转换成枚举类型。如果这个整型数据超过了枚举定义的范围,那么转换的结果是未定义的。

enum Status {
    On = 0,
    Off
};
Status s = static_cast<Status>(3);

你也可以使用static_cast将子类转换成基类,也可以将基类转换成子类。但将基类转换成子类是不安全的。

class B {};    
class D : public B {};  
void f(B* pb, D* pd) {  
   D* pd2 = static_cast<D*>(pb);   // 不安全,D可能有B没有的数据或方法  
   B* pb2 = static_cast<B*>(pd);   // 安全
}  

const_cast

const_cast用来删除const属性。

#include <iostream>
class B {
public:
    int num = 5;
};
int main() {
    using namespace std;
    const B b1;

    b1.num = 10; // Error: assignment of member 'B::num' in read-only object
    cout << b1.num << endl;

    B b2 = const_cast<B>(b1); // Error: invalid use of const_cast with type 'B', 
                              // which is not a pointer, reference, nor a pointer-to-data-member type
    b2.num = 15;
    cout << b1.num << endl;

    B *b3 = const_cast<B *>(&b1); // OK 
    b3->num = 20;
    cout << b1.num << endl;

    B &b4 = const_cast<B &>(b1); // OK
    b4.num = 25;
    cout << b1.num << endl;
}

另外一个MSDN上的例子:

#include <iostream>
using namespace std;
class CCTest {
public:
   void setNumber( int );
   void printNumber() const;
private:
   int number;
};

void CCTest::setNumber( int num ) { number = num; }

void CCTest::printNumber() const {
   cout << "\nBefore: " << number;
   const_cast< CCTest * >( this )->number--;
   cout << "\nAfter: " << number;
}

int main() {
   CCTest X;
   X.setNumber( 8 );
   X.printNumber();
}

在第二个例子中,我们也可以使用mutable关键字来进行处理。

dynamic_cast

dynamic_cast 通常用于转换多态类型

其目的是确保类型转换的结果是所请求类的有效完整对象。

将子类转换成基类时,dynamic_cast 都是成功的。

class B { };  
class C : public B { };  
class D : public C { };  
  
void f(D* pd) {  
   C* pc = dynamic_cast<C*>(pd);   // ok: C is a direct base class  
                                   // pc points to C subobject of pd   
   B* pb = dynamic_cast<B*>(pd);   // ok: B is an indirect base class  
                                   // pb points to B subobject of pd  
} 

上面例子的这种转换我们通常叫做向上转换,向上转换是一种隐式转换。因此这种情况下,我们使用static_cast或者dynamic_cast都是一样的。

我们再来看一个向下转换(将基类转换成子类)的例子:

#include <iostream>
class B {virtual void f(){}};
class D : public B {};

int main() {
    B* b = new D;
    B* b2 = new B;

    D* d = dynamic_cast<D*>(b);
    if (d == nullptr) {
        std::cout << "null pointer on first type-casting." << std::endl;
    }
    D* d2 = dynamic_cast<D*>(b2);
    if (d2 == nullptr) {
        std::cout << "null pointer on second type-casting." << std::endl;
    }
    return 0;
}

显然,输出结果是

null pointer on second type-casting.

再来看一个子类之间转换的例子:

#include <iostream>
class A {
public:
    int num = 5;
    virtual void f(){}
};

class B : public A {};

class D : public A {};

int main() {
    B *b = new B;
    b->num = 10;
    D *d1 = static_cast<D *>(b); // Error: invalid static_cast from type 'B*' to type 'D*'
    D *d2 = dynamic_cast<D *>(b); // OK
    std::cout << (d2 == nullptr) << std::endl; // 1
    delete b;
}

一个比较复杂的例子:

class A {virtual void f();};  
class B : public A {virtual void f();};  
class C : public A { };  
class D {virtual void f();};  
class E : public B, public C, public D {virtual void f();};  
  
void f(D* pd) {  
   E* pe = dynamic_cast<E*>(pd);  
   B* pb = pe;   // upcast, implicit conversion  
   A* pa = pb;   // upcast, implicit conversion  
}  

reinterpret_cast

reinterpret_cast 用来进行强制的数据转换(内存的重新解释),不管他们转换的类型之间有没有关联。

class A {};
class B {};
A *a = new A;
B *b = reinterpret_cast<B*>(a);

误用 reinterpret_cast 操作符很容易变得不安全。 除非所需的转换本质上是低级的,否则你应该使用其他强制转换运算符。

但是,它不能删除const属性。

class A {};
int main() {
    const A a;
    A *b = reinterpret_cast<A*>(&a); // Error: reinterpret_cast from type 'const A*' to type 'A*' casts away qualifiers
}

思考一下

  1. 下面的例子中,哪些地方会有编译错误?

    class A {
    public:
        virtual void foo() { }
    };
    class B {
    public:
        virtual void foo() { }
    };
    class C : public A , public B {
    public:
        virtual void foo() { }
    };
    void bar1(A *pa) {
        B *pc = dynamic_cast<B*>(pa);
    }
    void bar2(A *pa) {
        B *pc = static_cast<B*>(pa);
    }
    void bar3() {
        C c;
        A *pa = &c;
        B *pb = static_cast<B*>(static_cast<C*>(pa));
    }
    int main() {
        return 0;
    }
    
  2. 下面的模板函数是安全的吗?如果不是,更好的做法是什么?

    template <class T>
    unsigned char* alias(T& x) {
        return (unsigned char*)(&x);
    }
    

Reference

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容