initialization-order

Using a free "char const*" at static initialization time

Initialization order of free objects is undefined in C++. But what about the following? namespace foo { char const* str = "hey"; struct A { A() { cout << str; } } obj; } Is this still undefined behavior, or is there a special provision for pointers initialized with string literals? Aside from that: what if str was...

C++ static initialization order

When I use static variables in C++, I often end up wanting to initialize one variable passing another to its constructor. In other words, I want to create static instances that depend on each other. Within a single .cpp or .h file this is not a problem: the instances will be created in the order they are declared. However, when you wan...

C++: Construction and initialization order guarantees

Hi, I have some doubts about construction and initialization order guarantees in C++. For instance, the following code has four classes X, Y, Z and W. The main function instantiates an object of class X, which contains an object of class Y, and derives from class Z, so both constructors will be called. Additionally, the const char* para...