Passing constructor arguments to a singleton template

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
 
P

Pelle Beckman

Victor Bazarov skrev:
Pelle said:
[..]
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.
 
V

Victor Bazarov

Pelle said:
[..]
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
 
P

Pelle Beckman

Pelle Beckman skrev:
Victor Bazarov skrev:
Pelle said:
[..]
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.
 
P

puzzlecracker

Pelle said:
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?
 
A

Axter

Pelle said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top