csdnceshi62 2008-08-26 13:20 采纳率: 100%
浏览 315
已采纳

常规施法 vs 静态施法 vs 动态施法[复制]

This question already has an answer here:

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.

MyClass *m = (MyClass *)ptr;

all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?

MyClass *m = (MyClass *)ptr;
MyClass *m = static_cast<MyClass *>(ptr);
MyClass *m = dynamic_cast<MyClass *>(ptr);
</div>

转载于:https://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast

  • 写回答

8条回答 默认 最新

  • Memor.の 2009-08-10 13:50
    关注

    static_cast

    static_cast is used for cases where you basically want to reverse an implicit conversion, with a few restrictions and additions. static_cast performs no runtime checks. This should be used if you know that you refer to an object of a specific type, and thus a check would be unnecessary. Example:

    void func(void *data) {
      // Conversion from MyClass* -> void* is implicit
      MyClass *c = static_cast<MyClass*>(data);
      ...
    }
    
    int main() {
      MyClass c;
      start_thread(&func, &c)  // func(&c) will be called
          .join();
    }
    

    In this example, you know that you passed a MyClass object, and thus there isn't any need for a runtime check to ensure this.

    dynamic_cast

    dynamic_cast is useful when you don't know what the dynamic type of the object is. It returns a null pointer if the object referred to doesn't contain the type casted to as a base class (when you cast to a reference, a bad_cast exception is thrown in that case).

    if (JumpStm *j = dynamic_cast<JumpStm*>(&stm)) {
      ...
    } else if (ExprStm *e = dynamic_cast<ExprStm*>(&stm)) {
      ...
    }
    

    You cannot use dynamic_cast if you downcast (cast to a derived class) and the argument type is not polymorphic. For example, the following code is not valid, because Base doesn't contain any virtual function:

    struct Base { };
    struct Derived : Base { };
    int main() {
      Derived d; Base *b = &d;
      dynamic_cast<Derived*>(b); // Invalid
    }
    

    An "up-cast" (cast to the base class) is always valid with both static_cast and dynamic_cast, and also without any cast, as an "up-cast" is an implicit conversion.

    Regular Cast

    These casts are also called C-style cast. A C-style cast is basically identical to trying out a range of sequences of C++ casts, and taking the first C++ cast that works, without ever considering dynamic_cast. Needless to say, this is much more powerful as it combines all of const_cast, static_cast and reinterpret_cast, but it's also unsafe, because it does not use dynamic_cast.

    In addition, C-style casts not only allow you to do this, but they also allow you to safely cast to a private base-class, while the "equivalent" static_cast sequence would give you a compile-time error for that.

    Some people prefer C-style casts because of their brevity. I use them for numeric casts only, and use the appropriate C++ casts when user defined types are involved, as they provide stricter checking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况