Use super class address / pointer in initialization list

context 1: class D: public B1, public B2 {};

context 2: B2 takes B1 to initialize: B2 (B1 *) // B2 constructor

my question is on the D initialization list:

D :: D (): B1 (), B2 (?) ... What should be?

I do not want to put "(B1 *) this" in? because it is not recommended to use "this" in the initialization list. And since part B1 has been initialized, it makes sense to use it.

What should I do?

+3
source share
2 answers

B1 this, . §12.6.2/5: " , - ( mem)."

- - class D : public B1, public B2, mem-initializer - D::D() : B1(), B2( … ).

, " ".

EDIT: , , <this undefined , . , , , . 7: " mem- , mem-initializer. "

B2 B1, , .

class B1 {};
class B2 : virtual B1 {}; // under the hood, B2 has a pointer to B1.
class D : public virtual B1, public B2 {}; // D has a pointer too
 // Only the most-derived class (the one actually used for instantiation)
 // implements space for and initialization of the B1.
+3

. , B1 B2, , , , , , , .

- , ? , , - D B1 B2?

+1

Source: https://habr.com/ru/post/1740730/


All Articles