473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ 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<typena me T>
class CSingleton
{
public:
static T& Instance()
{
static T me;
return me;
}
};

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

*class MyClass: public CSingleton<MyCl ass>
*{
* *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::Instan ce();

when they meant

MyClass& copyOfMyClass = MyClass::Instan ce();

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<MyCl ass>
{
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<MyCl ass>;
};

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++.m oderated 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,puzzlecr acker写到:
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<typena me T>
class CSingleton
{
public:
static T& Instance()
{
static T me;
return me;
}
};

class MyClass: public CSingleton<MyCl ass{
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
5854
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? (Code below). I'm not really looking for thread safety or for something as general
7
12470
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
1
2448
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();
3
2475
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 what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
5
5305
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++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
2
3901
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 pointer. isn't child should have access to parent protected destructor? thank you. class CMachineFactory
3
5733
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 portable??? Will all compilers produce code that prints "single"? Instancing of object right in header file (that can be include multiple times - i.e. in multiple cpp files) give rise to my doubts... Singleton.cpp
5
5368
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
3040
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
1879
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 SINGLETON_HPP template<typename T> struct DefaultCreatorFunctor {
0
7883
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8263
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8021
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8254
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6677
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2393
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
1
1492
muto222
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.