F
ferdinand.stefanus
Hi
Could someone tell me what's the difference between these two singleton
implementations:
First implementation:
class Singleton
{
public:
static Singleton& instance() { return _instance; }
//... other members
private:
Singleton();
static Singleton _instance;
//... other members
};
Second implementation:
class Singleton
{
public:
static Singleton* instance() { return _instance; }
static void Initialize() { _instance = new Singleton; }
//... other members
private:
Singleton();
static Singleton* _instance;
//... other members
};
I know that in the first implementation, _instance will be put in the
static memory, and it will always be present, whereas in the second
implementation, memory will only be allocated on the heap after
Singleton::Initialize() is called. But aside from that, is there any
practical difference between the two, like on what circumstances is the
first implementation preferred over the second (or vice versa)?
Thanks!
Could someone tell me what's the difference between these two singleton
implementations:
First implementation:
class Singleton
{
public:
static Singleton& instance() { return _instance; }
//... other members
private:
Singleton();
static Singleton _instance;
//... other members
};
Second implementation:
class Singleton
{
public:
static Singleton* instance() { return _instance; }
static void Initialize() { _instance = new Singleton; }
//... other members
private:
Singleton();
static Singleton* _instance;
//... other members
};
I know that in the first implementation, _instance will be put in the
static memory, and it will always be present, whereas in the second
implementation, memory will only be allocated on the heap after
Singleton::Initialize() is called. But aside from that, is there any
practical difference between the two, like on what circumstances is the
first implementation preferred over the second (or vice versa)?
Thanks!