You cannot do this with objects because of the object slicing problem. You need to use pointers instead - preferably, smart pointers, such as unique_ptr<Parent>
:
GenericCollection<unique_ptr<Parent>> collection;
Now you can implement your getElements
method that uses Run-Time Type Information (RTTI) to detect the type of the object pointed to by the smart pointer, and keep only the ones pointing to Child2
.
Note that in order to use RTTI your base class Parent
needs to have at least one virtual member function. This shouldn't be an issue in your case, because you expect polymorphic behavior.