C++定义和强制转换

来源:互联网 发布:linux shell 控制语句 编辑:程序博客网 时间:2024/05/01 10:14

  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 }
0 0
原创粉丝点击