P
Pelle Beckman
Hi,
I've done some progress in writing a rather simple
singleton template.
However, I need a smart way to pass constructor arguments
via the template.
I've been suggested reading "Modern C++ Design" or similar
books, but I feel there are full of clever guys here
who could help me out.
Also, c++ experts seem to have this kinky obsession with
references instead of pointers, so if you have
a argument (and implementation) for using them, please
tell me.
Here's what I've done so far.
template<class T>
class Singleton {
public:
T* Instance ();
void Release ();
Singleton ();
~Singleton ();
private:
static T* m_singleton;
};
template<class T> T* Singleton<T>::m_singleton = 0;
template<class T>
Singleton<T>::Singleton () {
}
template<class T>
Singleton<T>::~Singleton () {
// ...
}
template<class T>
T* Singleton<T>::Instance () {
if (m_singleton == 0) {
m_singleton = new T;
}
return m_singleton;
}
template<class T>
void Singleton<T>::Release () {
if (m_singleton == 0)
return;
delete m_singleton;
m_singleton = 0;
}
Thanks.
-- Pelle
I've done some progress in writing a rather simple
singleton template.
However, I need a smart way to pass constructor arguments
via the template.
I've been suggested reading "Modern C++ Design" or similar
books, but I feel there are full of clever guys here
who could help me out.
Also, c++ experts seem to have this kinky obsession with
references instead of pointers, so if you have
a argument (and implementation) for using them, please
tell me.
Here's what I've done so far.
template<class T>
class Singleton {
public:
T* Instance ();
void Release ();
Singleton ();
~Singleton ();
private:
static T* m_singleton;
};
template<class T> T* Singleton<T>::m_singleton = 0;
template<class T>
Singleton<T>::Singleton () {
}
template<class T>
Singleton<T>::~Singleton () {
// ...
}
template<class T>
T* Singleton<T>::Instance () {
if (m_singleton == 0) {
m_singleton = new T;
}
return m_singleton;
}
template<class T>
void Singleton<T>::Release () {
if (m_singleton == 0)
return;
delete m_singleton;
m_singleton = 0;
}
Thanks.
-- Pelle