c++轉型
Prerequisites:
以下C語言的強制轉型
語法
(類型)表達式
類型(表達式)
1
2
3
4
int a = (int)8.3;
int a = int(8.3);
Student student2 = Student("student2");
Student student3 = (Student)"student2";
接下來要說C++的強制轉型
static_cast
語法
static_cast<類型>(表達式)
基本型態
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {
// int是4byte
int i = 10;
// long在win是4byte,在linux8byte
// 低精準度int轉成高精準度long,不會有小數點被截掉,精準度不足的問題
long long1 = i;
double d = 5.55;
// 自動轉型,double是8byte高精準度轉到long4byte低精準度
// 會有小數點被截掉,精準度不足的問題
long long2 = d;
// c語言的強制轉型
long long3 = (long)d;
// c++的強制轉型
long long4 = static_cast<long>(d);
return 0;
}
指標轉型
以下C語言的指標強制轉型
語法
(指標類型*)位址
以下C++的指標強制轉型,但static cast只支援同類型轉型 語法
static_cast<指標類型*>(位址)
不同類型要先轉成void指標(任何類型指標都能轉成void指標)
1
2
void* point_v1 = 變數位址;
double* 指標變數 = static_cast<類型*>(point_v1);
完整程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
int i = 10;
// c的強制轉型
double* point_d1 = (double*)&i;
// c++的強制轉型
// static cast不能轉型二種不同類型的指標
// 以下語法編譯不過,因為&i是整數類型的指標
//double* point_d2 = static_cast<double>(&i);
// 需要先把整數類型的指標轉成void類型的指標
//任何類型的指標都可以自動轉型成void指標
void* point_v1 = &i;
double* point_d3 = static_cast<double*>(point_v1);
return 0;
}
函式轉型指標類型
在函式中要轉型不同類型指標,函式參數要用void*
1
2
3
4
5
6
7
8
void func(void* ptr) {
double* point = static_cast<double*>(ptr);
}
int main() {
int i = 10;
func(&i);
return 0;
}
reinterpret_cast
以下三種語法例子,一定要有一方是指標,要嘛指標轉指標,要嘛指標轉成數值,要嘛數值轉成指標
轉型不同類型指標
語法1:不同類型指標轉型
reinterpret_cast<指標類型>(位址或指標)
轉型指標可以不用透過void指標,就直接把指標轉型。
1
2
3
4
5
int main() {
int i = 10;
double* point_double = reinterpret_cast<double*>(&i);
return 0;
}
基本類型與指標互相轉型
所謂的基本類型是指一定要跟指標一樣是8byte才能互轉,不能用int(4byte)轉指標(8byte),會編譯不過。
語法2:long long(8byte)轉成指標(8byte)
reinterpret_cast<指標類型>(基本類型)
語法3:指標轉成long long
reinterpret_cast<基本類型>(指標類型)
1
2
3
4
5
6
7
8
9
10
11
void func(void* ptr) {
// 指標轉成long long
long long ll = reinterpret_cast<long long>(ptr);
cout << "ll = " << ll << endl;
}
int main() {
long long ll = 10;
// long long(8byte)轉成void指標(8byte)
func(reinterpret_cast<void*>(ll));
return 0;
}
const_cast
static_cast與reinterpret_cast不能去掉const類型,但const_cast可以去掉const類型
基本資料型態去掉const
對於基本類型const轉成不是const類型,可以編譯成功
1
2
3
4
5
6
int main() {
// 對於基本類型沒有const轉成不是const類型的問題
const int i1 = 10;
int i2 = i1;
return 0;
}
const指標轉成不是const指標會編譯不過
若把const指標轉成不是const指標,以下程式碼會編譯不過,不能把const類型指標指派給不是const類型指標。
1
2
3
4
5
int main() {
const int* ptr_i1 = nullptr;
int* ptr_i2 = ptr_i1;
return 0;
}
const_cast語法
以下C語言的指標去掉const
語法
(指標類型*)位址
以下C++的指標去掉const 語法
const_cast<指標類型*>(位址或指標)
使用const_cast強制轉型
1
2
3
4
5
6
7
8
int main() {
const int* ptr_i1 = nullptr;
// c語言去掉const,強制轉型成不是const
int* ptr_i2 = (int*)ptr_i1;
// c++強制轉型不是const
int* ptr_i3 = const_cast<int*>(ptr_i1);
return 0;
}
非const類型轉成const
1
2
3
4
5
int main() {
char *p1 = "321";
const char *c = const_cast<const char*>(p1);
return 0;
}