随笔 - 11  文章 - 0  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔分类

随笔档案

相册

关注

网站

搜索

  •  

最新评论

阅读排行榜

评论排行榜

C++有四种类型转换操作符:static_cast、const_cast、dynamic_cast、reinterpret_cast。
1>. static_cast普通转换,如double转int:
1 double a = 10; 2 int b = static_cast<int>(a);
2>. const_cast改变const或者volatile属性:
1 const char* szA = "test"; 2 char* szB = const_cast<char*>(szA);
3>. dynamic_cast把指向基类的指针或引用转换成指向派生类或者基类的兄弟类的指针或引用:
1 class Base 2 { 3 public: 4 virtual void test(){} //基类必须有虚函数 5 }; 6 7 class Child : public Base 8 { 9 public: 10 void print(){ cout << "child::base" << endl; } 11 }; 12 13 Base* pBase = new Base(); 14 Child* pChild = dynamic_cast<Child*>(pBase); 15 pChild->print();
4>. reinterpret_cast用来在函数指针之间进行类型转换
1 typedef void (*FuncPtr)(); 2 FuncPtr funcPtrArray[10]; 3 int doSomeing(){return 0;} 4 5 funcPtrArray[0] = reinterpret_cast<FuncPtr>(&doSomeing);
posted on 2011-03-07 14:28 郭小帅 阅读(288) 评论(0)  编辑 收藏 引用 所属分类: C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理