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:

static initialization order fiasco


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -