user630983
user630983

Reputation: 947

object initialization

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.

Upvotes: 4

Views: 788

Answers (2)

Krit
Krit

Reputation: 558

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).

Upvotes: 0

vissi
vissi

Reputation: 2334

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.

Upvotes: 2

Related Questions