luk32
luk32

Reputation: 16090

Is it possible to initialize vector to be copy of another with appended extra elements

I have class with a static std::vector<string>. I would like to derive from it, and extend the vector in the derived class.

Something like this:

class A {
  static std::vector<std::string> column_names;
};
std::vector<std::string> A::column_names = {"col1", "col2"};

class B : public A{
  static std::vector<std::string> column_names;
};
std::vector<std::string> B::column_names = {A::column_names, "col2"}; // <-- *

Is something like * possible?

My rationale is that, I would like to initialize the B::column_names with out delegating it into constructor with some static flag. B will always be some extension to A so its natural to append the columns to it.

Edit: Also I don't plan on changing of column_names during run-time. It can be defined as const if that helps with anything.

Upvotes: 1

Views: 82

Answers (1)

Nim
Nim

Reputation: 33655

Could try having a static function in B which returns the vector with the correct values, for example..

class B
{
  static std::vector<std::string> initial()
  {
    auto v = A::column_names;
    v.push_back("col2");
    return v;
  }
};
// Now initialize column_names from this function...
std::vector<std::string> B::column_names = B::initial();

Upvotes: 1

Related Questions