468,754 Members | 1,346 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,754 developers. It's quick & easy.

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 1642
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sean Dettrick | last post: by
7 posts views Thread by Tim Clacy | last post: by
1 post views Thread by Jim Strathmeyer | last post: by
3 posts views Thread by Alicia Roberts | last post: by
5 posts views Thread by Pelle Beckman | last post: by
3 posts views Thread by Raider | last post: by
5 posts views Thread by tobias.sturn | last post: by
15 posts views Thread by Nick Keighley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.