Looking for a better way to handle instances

A friend showed me how to create instances something like a year ago, but my style of handling them hasn't changed in a very long time. I would like to know if there is currently a better way of doing it.

1
2
3
4
5
6
7
8
9
10
11
//.h
class cTest{
public:
 static cTest* Instance();
 static void Destruct();
private:
 static cTest *mInstance;

 cTest(){}
 ~cTest(){}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//.cpp
cTest cTest::mInstance = 0;

cTest* cTest::Instance()
{
  if(mInstance)
    return mInstance;
  return mInstance = new cTest;
}

void cTest::Destruct()
{
  if(mInstance)
    delete mInstance;
}


Thats an implementation of the Singleton design pattern. It's still used, albeit you can do it 100 diff ways. The way I do it is very similar to that (as a prof developer). Just be aware that it's not thread-safe.

You can google "Singleton design pattern" and see other ways to implement it. Nothing wrong with that way though.
Topic archived. No new replies allowed.