vendredi 23 décembre 2022

"A constructor can initialize members and bases of its class, but not members or bases of its members or bases"? [closed]

From the book "The C++ Programming Language - Fourth Edition" on the following pages:

p. 501:

For example:

struct B {B(int); /* ... */ };
struct BB:B {/* ... */ };
struct BBB: BB {
       BBB(int i) : B(i) {}; // error: trying to initialize base's base
       // ...
};

vs.

p. 491:

But why is the following code allowed, then?

struct Y : X {
       X m {0};

       Y(int a) : X{a}, m{a} {};
       Y()      : X{0}       {};
}

Aucun commentaire:

Enregistrer un commentaire