4

In the book of "The C++ Language", the author claimed

Sometimes, when you design a library, it is necessary, or simply convenient, to invent a type with a constructor and a destructor with the sole purpose of initialization and cleanup. Such a type would be used once only: to allocate a static object so that the constructor and the destructor are called.

I am interested which kind of scenario that this statement is referring to? Or how this statement helps the software design?

The book also gives an example

class Zlib_init{
    Zlib_init( );
    ~Zlib_init( );
};

class Zlib{
    static Zlib_init x;
}

And the book states that

Unfortunately, it is not guaranteed that such an object is initialized before its first use and destroyed after its last use in a program consisting of separately compiled units.

Why this can happen?

Thanks for clarification.

1
  • You have a guarantee that such an object will be created before main is called and destroyed after main returns. As such, you could be tempted to say "but, but... all is good". Unluckily, you could have a different static object somewhere in your program which also uses the Zlib_init object, and the C++ language makes no guarantee whether that one will be created or destroyed before or after this or any other object, as soon as they are in different compilation units. So, the wording is maybe a bit pedantic, but it is 100% correct. (Well, it's correct anyway, since it's Stroustrup). Commented Mar 16, 2011 at 16:39

2 Answers 2

2

The C++ standard does not specify the order in which the static objects are created. Therefore, if you need some hierarchy in static objects, you need them to depend one on another (e.g., one should be the member of the other). The construct from the book guarantees this behaviour.

For example, a hypothetical game engine needs sound and graphics engines to work, if you declare them as static objects in separate compilation units, and use one from another, there's no guarantee it wouldn't fail unless you code them the way you specified.

See C++ faq entry for the second part of your question.

Sign up to request clarification or add additional context in comments.

1 Comment

In the same compilation unit, they are constructed in the order of their definition (not declaration). The problem is when they are spread among multiple units...
0

Unfortunately, it is not guaranteed that such an object is initialized before its first use and destroyed after its last use in a program consisting of separately compiled units.

For example if you have an instance of your class on static storage in one module and want to use it from constructor of another class on static storage in another module. In this case you imply that the first instance will be initialized before the second one. But the language has no means to specify this order if the instances are defined in separate modules.

Sometimes, when you design a library, it is necessary, or simply convenient, to invent a type with a constructor and a destructor with the sole purpose of initialization and cleanup. Such a type would be used once only: to allocate a static object so that the constructor and the destructor are called.

This is useful when you work with 3-rd partly libs requiring initialization and finalization calls. For example WinSock 2 requires WSAStartup before you can call other WSA functions and WSACleanup when you're done with WinSock in your process. If you have a static instance of this kind of class calling WSAStartup in constructor and WSACleanup in destructor, you should be able to use WSA functions in other places of your program (except constructors/destructurs of other static objects).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.