Base class pointer vs inherited class pointer?

Posted by Goose Bumper on Stack Overflow See other posts from Stack Overflow or by Goose Bumper
Published on 2010-04-23T17:51:11Z Indexed on 2010/04/23 17:53 UTC
Read the original article Hit count: 305

Filed under:
|
|

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

    Animal *a = new Dog();
    Dog *d = new Dog();

In one, the pointer is for the base class, and in the other, the pointer is for the derived class. But when would this distinction become important? For polymorphism, either one would work exactly the same, right?

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance