随笔 - 11  文章 - 0  trackbacks - 0
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔分类

随笔档案

相册

关注

网站

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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 @ 2011-03-07 14:28 郭小帅 阅读(288) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-03-05 20:24 郭小帅 阅读(577) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-03-01 22:51 郭小帅 阅读(534) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-03-01 21:15 郭小帅 阅读(1397) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-02-27 21:30 郭小帅 阅读(2234) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-02-27 17:23 郭小帅 阅读(440) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-02-27 16:30 郭小帅 阅读(372) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-02-27 14:26 郭小帅 阅读(549) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-02-26 18:33 郭小帅 阅读(1912) | 评论 (0)编辑 收藏
     摘要:   阅读全文
posted @ 2010-02-26 17:27 郭小帅 阅读(1315) | 评论 (0)编辑 收藏
仅列出标题  下一页