473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

singleton template class

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()
{
static T me;
return me;
}
};

class MyClass: public CSingleton<MyClass>
{
public:
MyClass(){};
~MyClass(){};
void Print() { printf("testing %d\n",val); }
int val;
};
Aug 28 '08 #1
2 1829
On 28 Aug, 18:12, puzzlecracker <ironsel2...@gmail.comwrote:
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()
* *{
* * *static T me;
* * *return me;
}
*};

*class MyClass: public CSingleton<MyClass>
*{
* *public:
* * *MyClass(){};
* * *~MyClass(){};
* ** void Print() { printf("testing %d\n",val); }
* ** int val;
*};
Did you mean make the ctor in the derived class PRIVATE, as well as
copy-ctor and assignment?

As your class stands, there's nothing to stop you from instantiating
an instance of MyClass directly. Or inadvertently copying the class.
Making the copy-ctor and assignment operator private would stop people
coding

MyClass copyOfMyClass = MyClass::Instance();

when they meant

MyClass& copyOfMyClass = MyClass::Instance();

But there's no neat way to hide the constructor, to prevent its direct
use. You could do something like this (inc. making CSingleton<a
friend).

class MyClass: public CSingleton<MyClass>
{
public:
~MyClass(){}

void Print()
{
printf("testing %d (%x)\n", val, (int)this);
}

int val;

private:
// stop direct construction
MyClass():val(0){}

// stop copy construction and copying
MyClass(const MyClass&);
MyClass& operator=(const MyClass&);

// allow CSingleton<to construct us
friend CSingleton<MyClass>;
};

It is more common to return a pointer from the Instance() method. This
helps with the copy-ctor/assignment related problems.

Also, you might be interested in the thread "Problem with Singleton
template" posted to comp.lang.c++.moderated on 20 Aug. this year
(2008, for people Googling in the future).

Andy
Aug 30 '08 #2
cch
于 Thu, 28 Aug 2008 10:12:24 -0700,puzzlecracker写到:
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()
{
static T me;
return me;
}
};

class MyClass: public CSingleton<MyClass{
public:
MyClass(){};
~MyClass(){};
void Print() { printf("testing %d\n",val); } int val;
};
Following code is one reference for you:
/
**================================================ ============================
* Copyright (C) 2006 Team RioWow
*
* File: singleton.h
* Description:
This file is to define and surpport different complier and system.
* Author: Rio Chang
* Update: 10/28/2006
================================================== ===========================*/
#ifndef SINGLETON_H
#define SINGLETON_H

template <class T>
class Singleton
{
public:
static T* getInstance()
{
return &(getSingleton());
}
static T& getSingleton()
{
static T instance;
return instance;
}
~Singleton(){};
protected:
Singleton(const Singleton& sig);
Singleton& operator = (const Singleton& sig);
Singleton(){};
}
;
#endif
//------------------------------------------------------------------------------
Aug 31 '08 #3

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 definitions - one that returns a reference, and one that...
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 arbitrary class a singleton without having to: a)...
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 g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
5
by: Pelle Beckman | last post by:
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++...
2
by: yccheok | last post by:
hello, in a singleton design, i trying to make my parent class having protected constructor and destructor. however, the compiler give me error when my child, trying to delete itself through parent...
3
by: Raider | last post by:
I need to have one object for each template argument(s) used. For example, I need to have one object of int. I tried the following code and it gives me all I want with Visual C++ 7.1. But is it...
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
15
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
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.