Hi,
I am playing around and trying to understand template singleton class and I accidentally created something that I am surprise it even runs.
What I don't understand is when did Apple and Orange instances get created?
Could you also comment on the good and bad of the Singleton class if it is proper?
// singleton template test
#include <iostream>
template <typename T> class Singleton
{
private:
Singleton(const Singleton<T>&){};
Singleton& operator=(const Singleton<T>&){};
protected:
static T* _mInstance;
static int i; // for debugging purposes
Singleton()
{
i++; // for debugging purposes
_mInstance = this;
// this function never called?
std::cout << "created" << std::endl;
};
public:
static T& getSingleton()
{
return *_mInstance;
}
static T* getSingletonPtr()
{
return _mInstance;
}
};
template <typename T> T* Singleton<T>::_mInstance;
template <typename T> int Singleton<T>::i = 0; // for debugging purposes
class Orange : public Singleton<Orange>
{
public:
void squeeze()
{
std::cout << "squeeze" << i << std::endl;
}
};
class Apple : public Singleton<Apple>
{
public:
void cut()
{
std::cout << "cut" << i << std::endl;
}
};
int main()
{
Orange::getSingletonPtr()->squeeze();
Apple::getSingleton().cut();
return 0;
}