472,353 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Passing constructor arguments to a singleton template

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
5 5132
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean Dettrick | last post by:
Hi, apologies for yet another Singleton posting. From reading around previous postings etc, I've cobbled together two Singleton template...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an...
2
by: Chr?stian Rousselle | last post by:
Hello, I want to do derive a class from a Singleton template base class. Is there a way to solve the following problem: template<class T>...
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and...
2
by: Alexander Stippler | last post by:
Hello, I want to write a non-template class with a constructor template member. The constructor shall not take arguments. Is this possible at...
15
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static...
2
by: keepyourstupidspam | last post by:
Hi, I am wondering if My Singleton class is ok from a thread safety point of view. Am i adding the thread locks in the right places? ...
4
by: Deep | last post by:
Can I use a class in this manner, where a constructor is of templated: template<typename T> class my_class{ public: int x_; public:...
2
by: puzzlecracker | last post by:
Will this function is singleton or do I need to declare a ctor in the derived class as public, as well as copy-ctor and assignment operator. ...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.