mardi 5 avril 2016

C++ delegating ctor and parent ctor with argument

This does not seem to work in C++11:

class B : public A
{
  public:
    B(const A& a)
        : A(a)   // parent constructor for passing the parameter
        , B()    // delegating constructor for init of other members
    {};
  // ...
};

gcc tells me that an initializer for a delegating constructor must appear alone.

How do I both call the constructor of the parent class with the parameter, and call the basic constructor of the B class? (I have a bunch of other constructors in B that need the same behavior).

Right now I am considering writing a private B::init() function and use it in all constructor bodies, but that tastes a bit much of C++03.

What is the preferred solution?

Aucun commentaire:

Enregistrer un commentaire