Coming back to my original issue:
Looking at the two different of code in your original post - there are fundamentally different.
If you look closely at the first few lines of the template version, you will see this:
1 2 3 4 5 6 7 8
|
template <class T>
class Singleton
{
public:
static T& Instance() {
static T _instance;
return _instance;
}
| |
Note very carefully that the Instance function does NOT create/return a reference object of type Singleton.
It returns an object of whatever type you create create the template with.
For example:
1 2 3
|
//some code
#include <string>
Singleton<string> myVar(6);
| |
then:
myVar::Instance()
will return a reference to a string
So no object of type Singleton has been created, so the Singleton destructor is not used and as it is a template, the destructor code is not even instantiated by the compiler, so
there is no issues with the destructor
HOWEVER:
For the non - template code you have:
1 2 3 4 5
|
class Singleton {
private:
static Singleton s;
int i;
| |
As you can see an object of type Singleton is actually created (s).
So for the two set of codes to be the same, the template class should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
template <class T>
class Singleton
{
public:
static Singleton& Instance() {
static Singleton _instance;
return _instance;
}
private:
Singleton() {};
~Singleton(){};
Singleton(Singleton const&);
Singleton& operator=(Singleton const&);
};
| |
PS - you edited your code.
I'll have a look at that link you posted later.