Pass abstract parameter to method, why not?

base-class, c++, inheritance, subclass

Solution

You need to specify the parameter as a pointer (or reference), e.g. `Abstract *` rather than `Abstract`. If `Derived` inherits from `Abstract`, you can pass a variable of type `Derived *` when `Abstract *` is expected, but you can't in general pass one of type `Derived` when `Abstract` is expected.

Problem

I've written an abstract class (with pure virtual functions), and I'd like to have a method accept one such class as a parameter. Given that the class is abstract, I understand that I couldn't pass an abstract class to any method, but why can't I pass a subclass of an abstract class to that method? How do I specify that a parameter should be any of the subclasses of a specified baseclass? This is all in C++.

Original source

Related problems