Best way to insert items from a Derived class's constructor into a Base class's private std::vector?

Posted by Will on Stack Overflow See other posts from Stack Overflow or by Will
Published on 2010-04-06T22:32:16Z Indexed on 2010/04/06 22:33 UTC
Read the original article Hit count: 244

Filed under:
|
|

I have these classes:

class Base
{
...
private:
std::vector<X> v;
};

class Derived
{
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: 1) Create a function in Base: InsertItem(X*) that will insert an item into the vector. 2) 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!

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl