It is well-known that a Dog
is an Animal
but List<Dog>
is not List<Animal>
(SO question)
Anyway, if we have a function
void f(Animal a1, Animal a2); // Java
void f(Animal * a1, Animal * a2) // C++
We can safely use f() passing in objects of type Dog / Dog*
Now suppose, we want to generalize it as if it has many arguments of type Animal
by using a List
of them (and not specifically a multiple argument list ... ).
The List
is not going to be modified by the function,
adding a Cat
to a List<Dog>
for instance.
This:
void fg (List<Animal>); // Java
void fg (List<Animal *>) // C++
is not going to allow a call passing in a List<Dog> / List<Dog*>
value.
What is the way to go?
May be in Java
void fg (List<? extends Animal>)
and in C++
template<T>
void fg (List<T>)
Is that ok?
Are there other alternatives?
Thanks.