K_GLOBAL_STATIC and other macros

Jos van den Oever jvdoever at gmail.com
Fri Apr 13 10:30:32 BST 2007


2007/4/13, Thomas Zander <zander at kde.org>:
> I'm looking for a way to;
> init the static to zero initially;
> Define a method like 'instance' or 'self' that either returns an existing
> one or creates a new one including some initialisation code.

This is really easy:
MyObject&
MyObject::self() {
    static MyObject object;
    return object;
}

This ensures the object has been initialized properly, even if it is
used in another global static objects initialization. Since 'new' is
not called, deallocation is also handled properly. The only thing that
is not guaranteed is the order in which the destructors  are called.

Cheers,
Jos




More information about the kde-core-devel mailing list