I have these classes:
class Base
{
...
private:
std::vector<X> v;
};
class Derived : public Base
{
Derived(X*, int n);
};
where the constructor of Derived is passed an array of item Xs, which I need to insert into my vector v in the Base class. (X is a smart pointer)
Currently I see two ways to do this:
- Create a function in Base:
InsertItem(X*) that will insert an
item into the vector (1 by 1) - Create a vector in Derived that contains the
full list, then get it into Base by
moving the entire vector.
I dont see any advantages to #2, but was wondering if #1 was a good solution, or if there are better ways to do this.
Thanks!
There is a third option if you can modify
Base. You could make the vectorprotectedwhich will allow base classes full access:Your base class can now directly operate on
v. If you need to expose much of the vector functionality toDerived, this is the easiest way.If not, I would just add the one function (i.e. option #1) and would make it protected: