generatorglukoff 12.02.2013 13:05 Воркота

Base-from-member idiom:
In C++, base classes are initialized before any member of the derived classes. The reason for this is that members of a derived class may use base part of the object. Therefore, all the base parts (i.e., all the base classes) must be initialized before members of the derived class. Sometimes, however, it becomes necessary to initialize a base class from a data member that is available only in the derived class. It sounds contradictory to the rules of C++ language because the parameter (a member of derived class) that is passed to the base class constructor must be fully initialized. This creates circular initialization problem.
This idiom makes use of the fact that base classes are initialized in the order they are declared. The derived class controls the order of its base classes, and in turn, controls the order in which they are initialized. In this idiom, a new class is added just to initialize the member in the derived class that is causing the problem. This new class is introduced in the base-class-list before all other base classes. Because the new class comes before the base class that needs the fully constructed parameter, it is initialized first and then the reference can be passed as usual.

1. ulidtko 12.02.2013 15:42


Do you really want to delete ?