What is dynamic type of object

前端 未结 4 1564
温柔的废话
温柔的废话 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:24

    What i think is that dynamic type means dynamically allocated object using new.

    Nope.

    The dynamic type is the real type of an object that might be accessed via a reference (pointer included) that point to a base type of its real type.

    That is, if we have :

    class A {
    
    };
    
    class B : public A { };
    
    
    B l;
    A& k = l;
    

    Here k is a reference to an object of type A, but the real type of the referred object, its dynamic type, is B.

    Here "dynamic" has the meaning of "known only at run-time".

提交回复
热议问题