Base class pointer vs inherited class pointer?

后端 未结 8 1124
独厮守ぢ
独厮守ぢ 2021-01-05 23:11

Suppose I have a class Dog that inherits from a class Animal. What is the difference between these two lines of code?

    Animal *a         


        
8条回答
  •  礼貌的吻别
    2021-01-05 23:47

    No, they aren't the same.

    The Dog pointer is not as polymorphic as Animal. All it can point to at runtime is a Dog or a subclass of Dog. If there are no subclasses of Dog, then the Dog runtime type and compile time types are the same.

    The Animal pointer can refer to any subclass of Animal: Dog, Cat, Wildebeast, etc.

提交回复
热议问题