How to copy a classes constructor arguments in derived class without retyping them all?

So lets say I have a base class and the constructor for it has 10 arguments, If I inherit from that class I need to retype all the base classes constructor arguments into the derived one and then pass those arguments to the constructor like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

// Created on Chays iPad.

#include <iostream>
using namespace std;

class A 
{
    public:
        A(int health, int armor): mHealth(health), mArmor(armor)
        {}

        int GetHealth() const { return mHealth; }
        int GetArmor() const { return mArmor; }

    private:
        int mHealth{};
        int mArmor{};
};

class B :public A
{
    public:
        B(int health, int armor, int ammo): A{health, armor}, mAmmo(ammo)
        {}

        int GetAmmo() const { return mAmmo; }

    private:
        int mAmmo{};
};

int main() 
{
    B b(100, 150, 90);

    cout << b.GetHealth() << endl;
    cout << b.GetArmor() << endl;
    cout << b.GetAmmo() << endl;

    return 0;
}


Is there any way to copy all of class A’s constructor arguments into B’s without having to retype everything or is that just what you have to do and there is no way around it?
Do you mean:
B::B( const A& base, int ammo): A{base}, mAmmo{ammo}
{}
If you want the constructor args for B as part of A's constructor, then that's how you do it. But if you want to pass a type A as an arg to B then as keskiverto's comment above.
Yes that was what I was looking for. Often I find that I have to re type the args in the derived classes and they dont change at all so I see no point in re-typing things that I wont change but maybe I am missing something, I dont know if there are any drawbacks to this.

Here's what I got.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

class A 
{
    public:
        A(int health, int armor): mHealth(health), mArmor(armor)
        {}

        int GetHealth() const { return mHealth; }
        int GetArmor() const { return mArmor; }

    private:
        int mHealth{};
        int mArmor{};
};

class B :public A
{
    public:
        B() = default;
        B(const A& b, int ammo): A{b}, mAmmo(ammo)
        {}

        int GetAmmo() const { return mAmmo; }

    private:
        int mAmmo{};
};

int main() 
{
    A a(100, 150);

    B b(a, 90);

    cout << b.GetHealth() << endl;
    cout << b.GetArmor() << endl;
    cout << b.GetAmmo() << endl;

    return 0;
}

Last edited on
Given:
1
2
3
4
5
6
B( const A& b, int ammo ); // #1
B( int health, int armor, int ammo ); // #2

  int he = 100;
  int ar = 150;
  int am =  90;

Their usage would be:
1
2
3
4
// #1
  B b( A(he, ar), am );
// #2
  B b( he, ar, am );

Yes, the #1 is simpler for you to write, but the user of B has to write more than with #2 and the user has to consult documentation of both A and B to know what data is required.
With #2 all is in one place, isn't it?

Who is more "important": the one, who writes a (good?) class, or the ones that use it?


Furthermore, there can be base classes that cannot be copied. Think of std::ostream. You won't make copies of it and other classes do derive from it.
Ok thank you for explaining. Is there ever a time where using #1 is a good idea? I'll stick to using #2, I was just curious to know different ways to do things.
Topic archived. No new replies allowed.