數(shù)據(jù)類型是對內(nèi)存的抽象椅野,在實際的開發(fā)過程中氮发,我們常常會遇到把一種類型轉(zhuǎn)換成另外一種類型的情況。
那么砸捏,在C/C++中谬运,類型轉(zhuǎn)換都有哪些玩法呢?
C 的類型轉(zhuǎn)換
寫法:(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++的類型轉(zhuǎn)換
把一種數(shù)據(jù)類型轉(zhuǎn)換成另外一種數(shù)據(jù)類型垦藏,可以是顯式的梆暖,也可以是隱式的。
C++中的隱式轉(zhuǎn)換
隱式轉(zhuǎn)換不需要任何轉(zhuǎn)換運算符膝藕。
他們通常自動發(fā)生在將一個類型的值拷貝給另外一個兼容的類型時式廷。
我們來看一個例子:
#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; // 隱式轉(zhuǎn)換
int x, y;
b.getXY(x, y);
std::cout << "x: " << x << " y: " << y << std::endl;
return 0;
}
在B b = a;
處發(fā)生了隱式轉(zhuǎn)換,程序輸出是
x: 10 y: 10
C++ 關(guān)鍵詞 explicit
C++11之后引入了關(guān)鍵詞explicit芭挽,如果加了explicit, 將不允許 隱式轉(zhuǎn)換 或 復(fù)制初始化.
我們?nèi)绻谏厦娴睦又屑尤雃xplicit
explicit B(A a) { x = a.getX(); y = a.getX(); }
將會發(fā)生編譯錯誤:
error: conversion from 'A' to non-scalar type 'B' requested
C++中的顯示轉(zhuǎn)換
我們可以像C一樣的方式進行強制轉(zhuǎn)換
B b = (B) a;
B b = B (a);
像上面一樣直接強轉(zhuǎn)而不使用轉(zhuǎn)換運算符滑废,有啥壞處呢?
它可能導(dǎo)致代碼編譯通過但運行出錯袜爪。就像下面的例子:
#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;
}
幾種轉(zhuǎn)換運算符
傳統(tǒng)的顯式類型轉(zhuǎn)換允許將任何指針轉(zhuǎn)換為任何其他指針類型蠕趁,而與它們指向的類型無關(guān)。這可能導(dǎo)致運行錯誤或其他意外的結(jié)果辛馆。
為了更好的控制類型轉(zhuǎn)換俺陋,C++提供了四種基礎(chǔ)的轉(zhuǎn)換運算符:
static_cast, const_cast, dynamic_cast, reinterpret_cast.
static_cast
static_cast
通常用于轉(zhuǎn)換非多態(tài)類型。
static_cast
轉(zhuǎn)換運算符可以用來執(zhí)行任何隱式轉(zhuǎn)換昙篙,包括標準類型和用戶定義的類型腊状。就像下面這樣:
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
轉(zhuǎn)換運算符可以將一個整型數(shù)據(jù)轉(zhuǎn)換成枚舉類型。如果這個整型數(shù)據(jù)超過了枚舉定義的范圍苔可,那么轉(zhuǎn)換的結(jié)果是未定義的缴挖。
enum Status {
On = 0,
Off
};
Status s = static_cast<Status>(3);
你也可以使用static_cast
將子類轉(zhuǎn)換成基類,也可以將基類轉(zhuǎn)換成子類焚辅。但將基類轉(zhuǎn)換成子類是不安全的映屋。
class B {};
class D : public B {};
void f(B* pb, D* pd) {
D* pd2 = static_cast<D*>(pb); // 不安全苟鸯,D可能有B沒有的數(shù)據(jù)或方法
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
關(guān)鍵字來進行處理早处。
dynamic_cast
dynamic_cast
通常用于轉(zhuǎn)換多態(tài)類型。
其目的是確保類型轉(zhuǎn)換的結(jié)果是所請求類的有效完整對象瘫析。
將子類轉(zhuǎn)換成基類時砌梆,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
}
上面例子的這種轉(zhuǎn)換我們通常叫做向上轉(zhuǎn)換贬循,向上轉(zhuǎn)換是一種隱式轉(zhuǎn)換么库。因此這種情況下,我們使用static_cast
或者dynamic_cast
都是一樣的甘有。
我們再來看一個向下轉(zhuǎn)換(將基類轉(zhuǎn)換成子類)的例子:
#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;
}
顯然诉儒,輸出結(jié)果是
null pointer on second type-casting.
再來看一個子類之間轉(zhuǎn)換的例子:
#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;
}
一個比較復(fù)雜的例子:
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
用來進行強制的數(shù)據(jù)轉(zhuǎn)換(內(nèi)存的重新解釋),不管他們轉(zhuǎn)換的類型之間有沒有關(guān)聯(lián)亏掀。
class A {};
class B {};
A *a = new A;
B *b = reinterpret_cast<B*>(a);
誤用 reinterpret_cast
操作符很容易變得不安全忱反。 除非所需的轉(zhuǎn)換本質(zhì)上是低級的,否則你應(yīng)該使用其他強制轉(zhuǎn)換運算符滤愕。
但是温算,它不能刪除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
}
思考一下
-
下面的例子中间影,哪些地方會有編譯錯誤注竿?
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; }
-
下面的模板函數(shù)是安全的嗎?如果不是魂贬,更好的做法是什么巩割?
template <class T> unsigned char* alias(T& x) { return (unsigned char*)(&x); }