By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Passing constructor arguments to a singleton template

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Pelle Beckman wrote:
[..]
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.
[..]


So, what you're saying is "I heard there was good book that explains
everything I need to know, but I am too damn lazy to read it, so why
don't you clever guys just spell it out for me?"

Or have I totally misunderstood the tone of your post? Then I am
genuinely sorry.

V
Jul 23 '05 #2

P: n/a
Victor Bazarov skrev:
Pelle Beckman wrote:
[..]
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.
[..]

So, what you're saying is "I heard there was good book that explains
everything I need to know, but I am too damn lazy to read it, so why
don't you clever guys just spell it out for me?"

Or have I totally misunderstood the tone of your post? Then I am
genuinely sorry.

V


Actually, it's more like "I would really like to read that book but
I don't have enough money to spend on it so I instead I'll ask
someone who knows".
But hey, I didn't know C++-programming isn't for the working class.
Jul 23 '05 #3

P: n/a
Pelle Beckman skrev:
Victor Bazarov skrev:
Pelle Beckman wrote:
[..]
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.
[..]


So, what you're saying is "I heard there was good book that explains
everything I need to know, but I am too damn lazy to read it, so why
don't you clever guys just spell it out for me?"

Or have I totally misunderstood the tone of your post? Then I am
genuinely sorry.

V

Actually, it's more like "I would really like to read that book but
I don't have enough money to spend on it so I instead I'll ask
someone who knows".
But hey, I didn't know C++-programming isn't for the working class.


Ok, I went a little bit too far there.
Sorry about that.
Jul 23 '05 #4

P: n/a

Pelle Beckman wrote:
Pelle Beckman skrev:
Victor Bazarov skrev:
Pelle Beckman wrote:

[..]
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.
[..]

So, what you're saying is "I heard there was good book that explains everything I need to know, but I am too damn lazy to read it, so why don't you clever guys just spell it out for me?"

Or have I totally misunderstood the tone of your post? Then I am
genuinely sorry.

V

Actually, it's more like "I would really like to read that book but
I don't have enough money to spend on it so I instead I'll ask
someone who knows".
But hey, I didn't know C++-programming isn't for the working class.


Ok, I went a little bit too far there.
Sorry about that.


Hmm, I am confused: is there a way to pass arguments to templates???

are they specialized? or utterly lost?

Jul 23 '05 #5

P: n/a
Pelle Beckman wrote:
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

A Singleton should not have public constructors as in your above class.
If this is a singleton wrapper class, then IMHO, it's not practical to
create a singleton that requires constructor arguments.
You're singleton should also have copy constructor and assignment
operator private or protected.
It should also have the destructor private or protected, however some
compilers like VC++ have problems compiling class with private or
protected destructors.

The following Singleton wrapper class has protected destructor. It
should compile on any compliant compiler, and it also compiles on VC++
6.0
Using this wrapper class you can make any class a singleton by either
inheriting from the Singleton wrapper class, or by calling
Singleton::Instance with a template type.

#include <stdlib.h>

template<typename T>
class Singleton
{
protected:
Singleton(){}
~Singleton(){}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
public:
class FriendClass
{
public:
FriendClass():m_MyClass(new T()){}
~FriendClass(){delete m_MyClass;}
T* m_MyClass;
};
static T& Instance() {
static FriendClass Instance;
return *Instance.m_MyClass;
}
};

class Widget {
private:
Widget(){}
~Widget(){}
Widget& operator=(const Widget&);
Widget(const Widget&);
public:
friend class Singleton<Widget>::FriendClass;
int m_i;
};
class foo : public Singleton<foo>{
private:
foo(){}
~foo(){}
foo& operator=(const foo&);
foo(const foo&);
public:
friend class FriendClass;
int m_i;
};
int main(int argc, char* argv[])
{
Widget& MyWidget = Singleton<Widget>::Instance();
foo& Myfoo = foo::Instance();

system("pause");
return 0;
}

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.