Advertisement

Simple inheritance question

Started by January 26, 2003 02:27 PM
2 comments, last by viper35al 21 years, 9 months ago
if I had the code Mammal *pDog = new Dog;, Mammal being a base class and dog being a derrived one, could pDog be a pointer to any derrived class of Mammal and still work the same as if I did something like Dog * pDog = new Dog? Thanks.
No. only dogs (or classes derived from dogs) work as dogs. Lets say you had another class Cat, derived from Mammal. Then the cat could not do dog stuff. The only thing it has in common with dogs is the stuff from Mammel.



Jacob Marner, M.Sc.
Console Programmer, Deadline Games
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Advertisement
oops,
I wasn''t thinking when I wrote that first post, but suppose I did something like Mammal *pM = new Cat, then did delete pM; then could I do pM = new Dog, if both dog and cat are derrived of Mammal?

Thanks.

yes you could do that, it would be fine

This topic is closed to new replies.

Advertisement