dynamic_cast和static_cast终极辨析

static_cast:强制类型转换cast。因此,当然可以用于有继承关系的类之间的cast,细分有三类:
upcast:Just same as dynamic_cast. 由于不用做runtime类型检查,效率比dynamic_cast高;
downcast:不安全得很。不建议使用。
crosscast:不能用。带来编译错误。

dynamic_cast:有继承关系的类之间的cast。非常安全,但是由于需要做runtime类型检查,并且需要virtual table的支持,效率低些。细分三类:
upcast:ok。static_cast就完全够了。
downcast:必须用它。这样,当做不正确的downcast时才能得到NULL指针,,而不会像static_cast那样得到一个可以用的其实是没有用的非空指针。
crosscast:只能用它才能实现。
posted @ 2008-06-30 17:17  能巴  阅读(635)  评论(0编辑  收藏  举报