473,569 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::S ingleton () {
}

template<class T>
Singleton<T>::~ Singleton () {
// ...
}

template<class T>
T* Singleton<T>::I nstance () {
if (m_singleton == 0) {
m_singleton = new T;
}
return m_singleton;
}

template<class T>
void Singleton<T>::R elease () {
if (m_singleton == 0)
return;
delete m_singleton;
m_singleton = 0;
}
Thanks.

-- Pelle
Jul 23 '05 #1
5 5296
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>::S ingleton () {
}

template<class T>
Singleton<T>::~ Singleton () {
// ...
}

template<class T>
T* Singleton<T>::I nstance () {
if (m_singleton == 0) {
m_singleton = new T;
}
return m_singleton;
}

template<class T>
void Singleton<T>::R elease () {
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::Inst ance with a template type.

#include <stdlib.h>

template<typena me 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_MyC lass;
}
};

class Widget {
private:
Widget(){}
~Widget(){}
Widget& operator=(const Widget&);
Widget(const Widget&);
public:
friend class Singleton<Widge t>::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<Widge t>::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
5851
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 definitions - one that returns a reference, and one that returns a pointer. I understand the pointer one and it works, but the reference one doesn't work as I expect. Can anybody explain this to me? ...
7
12466
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 arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
2
3770
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> class CSingleton { public: static T& GetInstance(void)
1
2444
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 g++'s output. Any help? // singleton.h template <class T> class Singleton : public T { public: static T * Instance();
2
4972
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 all? Here the concrete example: A class which contains three vectors. There are several different methods to initialize these vectors(size and contents). These methods are the template parameter I...
15
3037
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static SingletonClass _instance; return &_instance; }
2
2048
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? template <typename T> class DLLEXPORT CSingleton
4
1824
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: template<typename U> my_class(const my_class<U>& other ) : x_(other.x_){} };
2
1837
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. template<typename T> class CSingleton { public: static T& Instance() {
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.