class A { }
class B : public A { }
std::vector<B*> things;
void Func1(const std::vector<B*>& Bthings) {}
void Func2(const std::vector<A*>& Athings) {}
Func1(things); // ok
Func2(things); // not ok
I’m having some problems with code like the above. I have a stl collection of pointers to objects, but I don’t want Func2 to be aware of the subclass. Is there a nice way to do this? I know that a std::vector<B*> isn’t a std::vector<A*> but I can’t think this is a rare problem. All I can think of is having 2 separate lists of A*s and B*s which just seems wrong?
It isn’t a rare problem. The solution is just around the corner though:
Granted, this probably isn’t what you were looking for but it’s the closest match. Mixing compile-time polymorphism (templates, overloading) and runtime polymorphism (function overriding and class inheritance) doesn’t mix well.
If the above is not acceptable in your case, your best bet is copying the whole vector.