c++ - How can I do some code work just like a singleton? -
possible duplicate:
c++ singleton design pattern.
how can create 1 instance of class , share instance header , source files without using singleton? can provide simple example?
you can this:
class sample { /*** code **/ public: sample(); void dowork(); int getvalue(); /*** other functions ***/ }; sample & oneinstance() { static sample instance; return instance; } //use oneinstance everywhere oneinstance().dowork();
note sample
not singleton, can use oneinstance()
function if it's 1 , same instance of sample
, use everywhere!
you can use initialize global variables this:
int g_somevalue= oneinstance().getvalue();
which cannot done global static
instance of sample
. because of this:
Comments
Post a Comment