| re: order of extern initialization?
Kyle Kolander wrote:[color=blue]
> I was looking over the C++ standard for this answer and didn't find it...
> probably not looking in the right spots.
> Here's a quick setup:
>
> // sn.h
> extern const std::string SOME_NAME;[/color]
'SOME_NAME' has external linkage.
[color=blue]
> // sn.cpp
> const std::string SOME_NAME = "SomeName";[/color]
'SOME_NAME' has external linkage and static storage duration and
is dynamically initialised (since it's not a POD).
[color=blue]
> /* sn.cpp is compiled and linked into libSomeName.so */
>
> // myClass.h
> #include <sn.h>
> ...
> std::string ary[1] = { SOME_NAME };[/color]
'ary' has static storage duration and is dynamically initialised.
[color=blue]
> // myClass.cpp
> ...
> string s1 = ary[0]; // "" -- empty string
> string s2 = SOME_NAME; // "SomeName"[/color]
Both 's1' and 's2' have static storage duration and are dynamically
initialised.
[color=blue]
> Is this expected behavior?[/color]
Yes, just like any other. The order of initialisation of objects with
static storage duration defined in different translation units is simply
_unspecified_.
[color=blue]
> What is the order of initialization of global
> variables (ary) vs. an extern string defined in a source file that has
> already been compiled and linked into a library?[/color]
There is no such thing as "a library" in the language definition. It
is all platform-specific.
[color=blue]
> I admittedly have not
> spent enough time working with extern and generally shy away from global
> variables...[/color]
Not a bad idea.
V |