Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run member initializers before base-class constructor?

Normally one can change the order in which member initializers run by changing the order in which the members are declared in the class. However, is there a way to get the base class initializer/constructor to not run first?

This is a minimal sketch of my problem:

class SpecialA : public A {
public:
    explicit SpecialA(Arg* arg)
        : member(expensiveFunction(arg))
        , A(member) // <-- This will run first but I don't want it to
    {}
private:
    T member;
}
like image 333
Museful Avatar asked Dec 18 '22 21:12

Museful


1 Answers

No, it is not possible. Class initialization is always like that: base class, members, this class constructor.

The reason for this is simple - since you can reference your members in this class constructor, you have to construct members before you call your constructor. Since you can reference your base class members from your members, you have to have them constructed before this class members.

like image 78
SergeyA Avatar answered Dec 24 '22 03:12

SergeyA