Quote:Original post by Asuralm
Is there anyway I can do this please? Or if I need to define a new list class, how can I do it please?
This is provably impossible, because a vector of "Derived" is not a vector of "Base" and cannot be used as if it were one. I can add a "Base" to a vector of "Base", but I cannot add a "Base" to a vector of "Derived". Thus, the operation you wish to perform is impossible.
What is possible, however, is to use a read-only sequence of "Derived" as if it were a sequence of "Base" (the read-only property makes this possible). Java and C# allow this more or less natively with generics, if I remember correctly, but you can achieve a similar effect in C++ using templates:
template <typename T>void algorithm(const std::vector<T> & v){ // Code which assumes that T is derived from Base}
For additional safety:
template <typename T>void algorithm(const std::vector<T> & v, boost::enable_if<boost::is_base_of<Base,T>::value>::type *dummy = 0){ // Code which assumes that T is derived from Base}