ChefZ -- 磨劍錄 (A Coder's Log)

慣看秋月春風 一壺濁酒喜相逢 古今多少事 皆賦談笑中
posts - 42, comments - 3, trackbacks - 0, articles - 6

checked_cast

Posted on 2008-12-05 20:27 chefZ 阅读(366) 评论(0)  编辑 收藏 引用
// checked_cast - Uses fast static_cast in Release build,
// but checks cast with an ASSERT in Debug.
//
// Typical usage:
//   class Foo { /* ... */  };
//   class Bar : public Foo { /* ... */  };
//   Foo * pFoo = new Bar;
//   Bar * pBar = checked_cast<Bar *>(pFoo);
 
template <class TypeTo, class TypeFrom>                                         
TypeTo checked_cast(TypeFrom p)                                                 
{ 
        ASSERT(dynamic_cast<TypeTo>(p));                                        
        return static_cast<TypeTo>(p);                                          
}                               

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