473,230 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1819
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
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.