Consider the following outline:
class Base { /* ... */ };
class Derived : public Base
{
public:
void AdditionalFunctionality(int i){ /* ... */ }
};
typedef
OK, first off, if you do this, you'll want to make sure that Base has a virtual destructor. Otherwise, you'll get undefined behavior when the vector goes out of scope. (The vector's destructor will call Base
's destructor for each of its elements. If any elements are really a Derived
-- KABOOM!) Other that that, what you've written is perfectly safe and valid.
But what's the point? If you have a container of objects, you want to be able to treat them all the same. (Loop over all of them and invoke a function on each, or whatever.) If you didn't want to treat them the same, why put them in one container? So you've got a vector that can hold pointers to Base or pointers to Derived -- how do you know which elements are of which type? Are you planning to just call dynamic_cast
on each element every time you want to call AdditionalFunctionality
to check to make sure that the element really points to a Derived
? That's neither efficient nor idiomatic, and it basically defeats the whole point of using inheritance. You might as well just be using a tagged union.
You're using the wrong tool for the job. When people told you not to do this, it wasn't because it was unsafe or invalid, it's because you'll just end up making your code more complex than it needs to be.