1

If I have a base class with a constructor :

class Base{
Base(std::optional<type0> a, std::optional<type1> b, std::optional<type2> c, std::optional<type3> d) : _a(a), _b(b), _c(c), _d(d)  { }

    private:
    std::optional<type0> _a;
    std::optional<type1> _b;
    std::optional<type2> _c;
    std::optional<type3> _d;
};

Can my derived classes derive this constructor with only some of the values and will it automatically map it to the right values? For example if I do:

class Derived{
    Derived(std::optional<type1> b, std::optional<type3> d) : Base(b, d) { }
};

Will it automatically map b to _b and d to _d?

callum arul
  • 159
  • 6
  • 1
    You have to pass arguments for each of the parameter. Like 4 arguments for `a_`, `b_`, `c_` and `d_`. – Jason Nov 07 '22 at 07:41
  • 1
    This has nothing to do with `optional`. – Jason Nov 07 '22 at 07:43
  • `std::optional` is a type like any other (`int`, `std::string`). `std::optional` is not a language feature. Just because `std::optional` appears in the parameter list of a function does not suddenly make some of the parameters optional. – j6t Nov 07 '22 at 07:52
  • To have default values to parameters of constructor you need those in constructor prototype like std::optional c = std::nullopt without those what you do is syntax error. – Öö Tiib Nov 07 '22 at 08:03

2 Answers2

1

Can my derived classes derive this constructor with only some of the values and will it automatically map it to the right values?

No, you must pass arguments corresponding to each of the parameters of the base class constructor unless some of them have a default argument. This means in your example, in the initializer list of the derived class ctor we should pass 4 arguments corresponding to parameters a, b, c and d of the base' ctor.

Jason
  • 36,170
  • 5
  • 26
  • 60
0

Unfortunately it's not the case in C++.

You have to pass all the parameters with the same order in definition of Base constructor.

Btw, std::optional is not what you think. Optinal means that variable may or may not contain a value. It doesn't mean that you may not pass values as arguments optionally.

brmRush
  • 67
  • 7
  • This won't work as it is invalid C++. – Jason Nov 07 '22 at 07:49
  • Which part do you think will not work ? – brmRush Nov 07 '22 at 07:52
  • This part: `Base(type0 A, b, type2 C, d)` won't work as it is incorrect. – Jason Nov 07 '22 at 07:52
  • Why not? That is not actual code btw. type0 and type2 can be treated as pseudo code. Whatever type that base class expects, you have to replace with them. Sorry for not specifying this, if that's the problem. – brmRush Nov 07 '22 at 07:53
  • 1
    Not because of `type0` and `type2` as I know that this is not supposed to be actual code. `Base(type0 A, b, type2 C, d)` is incorrect because you can't pass arguments by writing `typo A` and `type2 C`. For example we can pass arguments by writing `int A` and `int B`. See [this demo](https://onlinegdb.com/iAe_BlC8W) where I added a comment to let you know why this is invalid. – Jason Nov 07 '22 at 08:00
  • Lol, you're absolutely right. Thanks for the tip, I never tried but just assumed that it would work. – brmRush Nov 07 '22 at 08:16