-1

I got classes below:

enum class Kind {
  Monday,
  Tuesday,
};

class Day {
 public:
  Day(Kind kind) : kind_(kind) {}
 private:
  Kind kind_;
};

class Tuesday : public Day, public std::vector<int> {
 public:
  Tuesday(...) : std::vector<int>(...), Day(Kind::Tuesday) {}
};

As you can see, even the Tuesday class inherit from Day, it calls Day(Kind::Tuesday), so the constructor forms of Tuesday should be the same with std::vector<int>

But even the constructor form is the same, I have to re-write all the constructors to match the constructors of std::vector<int>, and append Day(Kind::Tuesday) to the end of each constructor.

All I want is to use Tuesday as a vector. So I just curious if there's an easy way to do this?

  • 1
    " I have to re-write all the constructors" not really clear. There is only a single constructor for `Tuesday` in your code – 463035818_is_not_an_ai Jul 03 '19 at 10:04
  • @formerlyknownas_463035818 I mean there's a lot of constructors in `std::vector`, I have to create constructors of `Tuesday` to match each of the constructors in `std::vector` –  Jul 03 '19 at 10:05
  • just pick the constructor you like and use it. Why do you think you have to provide all of them? Maybe your question is more clear if you show at least a second constructor. And please replace the `...` with real code – 463035818_is_not_an_ai Jul 03 '19 at 10:07
  • 5
    Deriving from `std::vector` is potentially a bad idea. – DeiDei Jul 03 '19 at 10:07
  • 3
    @reavenisadesk Why not just have one c'tor that takes an `std::vector` as an r-value reference? I.e. the user can construct it however they want. – George Jul 03 '19 at 10:08
  • @DeiDei https://stackoverflow.com/q/4353203 – L. F. Jul 03 '19 at 10:09
  • This whole example looks like a bad idea. Why is `Tuesday` a class? – Caleth Jul 03 '19 at 10:25
  • @Caleth it’s just an example which is similar to my current problem –  Jul 03 '19 at 10:36

1 Answers1

1

You could use a variadic template constructor:

template <typename... Types>
Tuesday(Types... args) : std::vector<int>(args...), Day(Kind::Tuesday) {}