Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use members initialized in the `: , ` portion of a constructor later in `: , `? [duplicate]

For example, is it safe to call Area() here:

Polygon::Polygon( Coord x0, Coord y0, Coord x1, Coord y1 )
    : m_BoundingBox( x0, y0, x1, y1 ), m_Area( m_BoundingBox.Area() )
{
}

That is, can one assume that members in the : , portion of a constructor are constructed and initialized in the order they are listed?

like image 274
Michael Avatar asked Aug 03 '15 23:08

Michael


People also ask

Will the constructor initialize based on class order or initializer list order?

The member initializer list for a class constructor allows members to be initialized to specified values and for base class constructors to be called with specific arguments. However, the order in which initialization occurs is fixed and does not depend on the order written in the member initializer list.

How do you initialize a class member in C++?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is the use of initializer list in C++?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

What is a member initializer list?

Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.


1 Answers

It depends on the order of the members m_BoundingBox and m_Area in the class definition.

The standard states:

12.6.2/10 In a non-delegating constructor, initialization proceeds in the following order:

— First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.

— Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).

— Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

So the members are not initialized in the order in which they appear in the mem-initializer, but on their odrer in the class definition.

Here you can see an online example of what happens if the elements are in the right or in the wrong order in the class.

like image 151
Christophe Avatar answered Nov 15 '22 23:11

Christophe