1

I just want to check this:

If I have a derived class that has no new members. In case that the base class has already a long constructor (many parameters), the creation of the constructor in the derived class that just throws all the parameters to the base constructor is somewhat a boring (and obviously not necessary) process. Is there a way to tell the compiler to do it automatically? I suppose not, but just needed to check. I haven't found anything on this topic but still sorry for a duplicate if this is the case...

//I'm not interested in IDE automatic generation features (although I would welcome an advice of that type either), just a compiler...

Aros
  • 157
  • 1
  • 13
  • maybe you should reconsider the derived class.. if it doesn't (seemingly) add value? – AndersK Aug 02 '13 at 17:48
  • I don't think so... It overrides the logic of inherited methods. I don't think that that's a bad thing... – Aros Aug 02 '13 at 22:10

3 Answers3

3

You could use constructor inheritance (available since C++11)

In the definition of the derived class write:

public:

  using base_class::base_class;

where base_class is your base class.

dieram3
  • 592
  • 3
  • 7
2

It really depends on the version of C++ you are targetting.

The first answer is that in C++11 it is easy:

  • you can either use inheriting constructors
  • or perfect forwarding

Implementation:

// Inheriting
struct D: B { using B::B; };

// Forwarding
struct D: B {
    template <typename... T>
    D(int a, T&&... args): B(std::forward<T>(args)...), _a(a) {}
    int _a;
};

As demonstrated, while more verbose perfect forward is also more versatile.


If you are stuck in C++03, though, all is not lost: just refactor the base constructor so it takes less parameters (ideally one):

// Before
struct B { B(int a, int b, int c, ...); };

// After
struct Pack { Pack(int a, int b, int c, ...); };
struct B { explicit B(Pack p); };

Easy Peasy.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • OK, well... I would come out with the "Pack" option but the reason I asked the question is also for the template use... I wanted to have a template that would override certain method in the class specified by the template argument but there's a problem that all of the classes have a different constructor and therefore i would not be able to make an instance of such it... – Aros Aug 02 '13 at 22:23
0

If you are initializing several parameters in the base constructor and their values are being passed to the constructor, you can overcome this problem without C++11 by implementing setX() functions instead of initializing the values in the constructor.

BaseClass::BaseClass(int arg1, int arg2, int arg3, ...) :
    _member1(arg1),
    _member2(arg2),
    _member3(arg3),
    ...
{}

Change it to:

BaseClass::BaseClass() :
    _member1(0),
    _member2(0),
    _member3(0),
    ...
{}

void BaseClass::setMember1(int value) { _member1 = value; }
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • That is arguably worse, though. 1/ Maintaining invariants is more and more difficult as you add mutation possibilities, also the class may not have a natural default initialization 2/ All call sites now have to call a bunch of `setX` methods 3/ Whereas before you would get a compiler error when adding a new parameter, giving you a chance to find all calling sites; now you won't get anything. Arguably, a single `set` method (multiple arguments) would solve 2/ and 3/, however 1/ would not be solved. – Matthieu M. Aug 02 '13 at 18:06
  • Arguably, yes. It depends on whether you actually *need* the arguments upon construction or not. I think the solutions above are all better, but this has definitely been a cleaner solution for *some* cases I have encountered. – Cory Klein Aug 02 '13 at 18:10