vendredi 28 avril 2017

Class with static linkage

If I recall correctly, static linkage means that a variable or function is local to its compilation unit. Meaning that there could be variables or functions with the same name and parameters in other compilation units.

I want this for a class.

Let's say I have multiple compilation units that need to ensure proper deletion at exit. So I use atexit handlers. But every compilation unit should put its own atexit-handler into place.
I did this by making a class like this:

class Init {
private:
    static Init* self;

    Init() {
        std::atexit(Init::cleanup);
    }

    static void cleanup() {
        // Do cleanup
    }
};
Init* Init::self = new Init;

But if I have multiple classes called Init in various CUs the linker gets confused. And the compiler won't let me do static class Init {...}.

How do I archive my cleanup (if possible with classes that are called Init)?

Aucun commentaire:

Enregistrer un commentaire