Reputation: 3646
Would the following code facilitate lazy initialization?
Or would the singletonInstance
be created as soon as somebody includes the header (or even at program startup time)?
class SingletonClass
{
private:
SingletonClass();
~SingletonClass();
public:
static const SingletonClass& Instance()
{
static SingletonClass singletonInstance;
return singletonInstance;
}
};
Upvotes: 15
Views: 11497
Reputation: 14392
This is known as the Meyers singleton and they are lazy instantiated.
There are some considerations:
Upvotes: 17
Reputation: 1
The SingletonClass
constructor will not be called earlier than somenone calls the Instance()
method.
Thus yes, it facilitates lazy initialization.
Upvotes: 10