This post is what I just read.
The way he implements Singleton in C++ confuses me. I got several Questions about it and here is his code:
template<typename T>
class Singleton {
public:
static T& getInstance() { //Question 1
return instance;
}
private:
static T instance;
};
class DebugLog : public Singleton<DebugLog> { //Question 2
public:
void doNothing() {}
};
Question
I think we should put the
static T& getInstance()
's definition outside of the class body, right?He tries to make
class DebugLog
a singleton class, but when he inheritsSingleton<DebugLog>
, theDebugLog
doesn't already exist, right? If right, then how can the template classSingleton
instantiate an un-existent class?