0

I'm not quite sure how should it be written correctly. Can anyone help please?

class A {
public:
    A(int x);
};

class B {
public:
    B(float y);
};

class Launcher : public A, public B
{
    Launcher() : A(int x), B(float y); // ?
};

int main()
{
    Launcher(1, true);
}
Den
  • 17
  • 3
  • What you have is close. You just need empty braces rather than a semicolon on your `Launcher` constructor. Oh, and remove the types in that line. So `Launcher() : A(x), B(y) {}` – Fred Larson Jan 27 '22 at 15:53
  • 3
    You have to explicitly list the parameters of the derived class constructor, it's not necessary that they pass through one-for-one, all kinds of computation can be done along the way. All of these are possible: `Launcher(int x, float y) : A(x), B(y) {}` `Launcher(int x) : A(x), B(x * 0.2f) {}` `Launcher() : A(7), B(42.0f) {}` `Launcher(std::pair p) : A(1 << p.first), B(std::sqrt(p.second)) {}` – Ben Voigt Jan 27 '22 at 15:58
  • @FredLarson But then what would the values of `x` and `y` be? – Nathan Pierson Jan 27 '22 at 15:59
  • 2
    Oops, your constructor needs to accept the parameters: `Launcher(int x, float y) : A(x), B(y) {}` – Fred Larson Jan 27 '22 at 15:59
  • @Ben Voigt works like magic. Thank you! – Den Jan 27 '22 at 16:05
  • I honestly tried to google it out, but probably haven't managed to formulate the query correctly. – Den Jan 27 '22 at 16:07

0 Answers0