What is dynamic type of object

前端 未结 4 1572
温柔的废话
温柔的废话 2020-12-15 10:37

What i think is that dynamic type means dynamically allocated object using new. In the following case, do you say p points to dynamic type or stati

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 11:16

    The static type is the type of the variable, which is the only type known at compile time (hence considered static - cannot change). The dynamic type is the type of the object that is actually being pointed at run-time. Dynamic here means it's only known at run time, which means it might change (namely one variable can point on various objects of various types).

    The use of new in this content is not relevant, as your own example shows. In your main, the static and dynamic type of d is Derived, because it's not a pointer or reference. p, however, has a static type of Base, but in your code, the dynamic type would be Derived.

提交回复
热议问题