Sunday 15 March 2015

c++ - Is there a way to assign a name to a singleton in the local scope? -



c++ - Is there a way to assign a name to a singleton in the local scope? -

i have method returns singleton.

// window.h static window &getsingleton(); // window.cpp window &window::getsingleton() { static window singleton; homecoming singleton; }

coming objective-c i'm used assigning singletons local scope variable names, example.

uiapplication *application = [uiapplication sharedapplication];

but doing similar thing in c++ causes new object created.

window window = window::getsingleton();

this happens because re-create constructor called, in case window(window const &windowcopy).

is there way around this, or have phone call singleton method when need interact it?

in c++,

window window = <some expression>

means along lines of "construct object of type window value of <some expression>. window::getsingleton() evaluates lvalue reference window, , used initialize new window object (except fact window not re-create constructable.)

what need refer static object created in window::getsingleton() function. function returns reference. need utilize reference window on lhs:

window& window = window::getsingleton(); ^

c++ singleton

No comments:

Post a Comment