C++定義和強制轉換

  1 #include<iostream>
  2 using namespace std;
  3 #include<cstdlib>
  4 //static_cast數值類型之間,有一方三void*的指針類型之間的轉換
  5 //const_cast用於臨時去掉const、volatile限制
  6 //reinterpret_cast人以兩種指針類型之間,指針與數值類型之間
  7 //dynamic_cast
  8 

  9

10 {

 11         int n = static_cast<int>(45.67);
 12         int* p = static_cast<int*>(calloc(sizeof(int),10));
 13         free(p); 
 14         const int k = n;
 15         cout<<"k = "<<k<<endl;
 16         const_cast<int&>(k) = 789;
 17         cout<<"k = "<<k<<endl;
 18         float f = 123.45;
 19         p = reinterpret_cast<int*>(&f);
 20         cout<<*p<<endl;
 21         n = int(12.34);
 22         cout<<" n = "<<n<<endl;
 23         n = int();//輸出0
 24         cout<<" n = "<<n<<endl;
 25         int m(100);
 26         cout<<" m = "<<m<<endl;
 27         int x();//函數聲明
 28         int (y) = 200;//儘量不用 容易和強制類型轉換混淆
 29         cout << " y = "<<y<<endl;
 30         return 0;
 31 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章