I have recently created a class inheriting from another preexisiting one
class Child: public Parent{
...
};
and another class that contains a reference to a vector of parents
// foo.h
class Foo{
private:
std::vector<Parent>& things;
public:
Foo(std::vector<Parent>& things);
// + a bunch of other member functions
};
The members are implemented in a separate file
// foo.cpp
Foo::Foo(std::vector<parent>& things):things(things){}
// ... other member functions of Foo
I want to make Foo
work with vectors of typestd::vector<Child>
as well.
I am aware I could make Foo a template
template<class c>
class Foo{
...
and add the following lines to the end of foo.cpp to avoid linker errors
template class Foo<Parent>;
template class Foo<Child>;
This is not ideal because in the future Child
likely not going to remain the only class inheriting from Parent
and touching Foo.cpp every time a child of Parent
is born seems not like an elegant solution to me.
Is there a better way, than making Foo
a template?