Access to singleton defined in shared library

Suppose I have the following architecture:

  • Dodo singleton class libdodo
  • The main program related libdodoand libponny; Main program calledDodo::instance()
  • Ponnyclass of libponny. It has headers for Dodosingleton

mainwindow.cpp

    #include "shared/dodo/dodo.h"
    // ...
    Dodo::instance()->setNumber(91);

And then after this call Ponnyclass is created (ponny.cpp)

ponny.cpp

    #include "shared/dodo/dodo.h"
    // ...
    bool is = (Dodo::instance()->number() == 91);
    // Will `is` be true?

So, can I do it this way?

+5
source share
2 answers

Since the definitions of the behavior of your singleton are located in its library, this means that the singleton instance will be unique and will exist in the compilation unit where it was created.

, libdodo Dodo.cpp, :

static Dodo& Dodo::instance()
{
    static Dodo dodo;
    return dodo;
}

, , , , Dodo::instance , , , , .

, , - , , Dodo::instance() . :
:
: ++ , !
: ++


, ++ 11 (§6.7.4) :

, .

, ;)

+3

singleton . DLL/Shared, , .

" " , , , .

+1

All Articles