Polymorphic classes in templates

Posted by soxarered on Stack Overflow See other posts from Stack Overflow or by soxarered
Published on 2011-03-20T14:51:17Z Indexed on 2011/03/20 16:09 UTC
Read the original article Hit count: 210

Filed under:
|
|

Let's say we have a class hierarchy where we have a generic Animal class, which has several classes directly inherit from it (such as Dog, Cat, Horse, etc..).

When using templates on this inheritance hierarchy, is it legal to just use SomeTemplateClass<Animal> and then shove in Dogs and Cats and Horses into this templated object?

For example, assume we have a templated Stack class, where we want to host all sorts of animals. Can I simply state Stack<Animal> s; Dog d; s.push(d); Cat c; s.push(c);

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates