This question seems like it might be somewhat common, but I didn't find anything when scowering StackOverflow or the interwebs.
I came across a method in a C++ class that takes a list of (for example) Parent
objects. For this example, assume that there are two classes that derive from Parent
: Child1
and Child2
.
For each object in the list, the method checks if the object is of type Child2
(via a IsOfType()
method that each class implements), and if so, it calls a method that is only provided by the Child2
class.
Is this an issue in that the list-processing method cannot treat each object the same? I've seen this done in other places as well, so it seems it might be a common practice to some degree.
One option might be to declare the Child2
method in the Parent
class so that all Parent
objects implement it. However, in this case, only the Child2
class would actually implement any behavior when overriding the method.
Your thoughts? Thanks in advance!