473,406 Members | 2,620 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,406 software developers and data experts.

singleton base class

Hi,
I want to create an abstract base class for singletons.
The base class should demand that the derived classes implement a static
instance member operation.
Is it possible to create a pure virtual static member function in a base
classe?
If not, is there an better elegant solution for this problem?

Thanks in advance,
Anthony Lansbergen
--
to reply e-mail change EdAsys into adesys
Jul 19 '05 #1
4 10320
"Anthony Lansbergen" <an*****@EdAsys.nl> wrote...
I want to create an abstract base class for singletons.
The base class should demand that the derived classes implement a static
instance member operation.
Is it possible to create a pure virtual static member function in a base
classe?
No. "virtual" and "static" contradict each other.
If not, is there an better elegant solution for this problem?


Think templates. If you define the entire class or some member
function to use the "instance" member function of the template
argument, then it will work as if you had that requirement for
a "virtual static".

template<class Derived> class MySingleton {
...
Derived::instance()
...
};

And the user of your template would do

class MySpecialSingleton : MySingleton<MySpecialSingleton> {
...
};

Victor
Jul 19 '05 #2

"Anthony Lansbergen" <an*****@EdAsys.nl> wrote in message
news:3f***********************@news.xs4all.nl...
Hi,
I want to create an abstract base class for singletons.
The base class should demand that the derived classes implement a static
instance member operation.
Is it possible to create a pure virtual static member function in a base
classe?
If not, is there an better elegant solution for this problem?


I'd recommend to use a templated wrapper for your singletons. Look at the
approaches in Modern Design C++ or you can use the following simple
solution:

template<class T, class Tag = void>
class CSingleton
{
private:
CSingleton(void) { }
~CSingleton(void) { }
CSingleton( const CSingleton& rhs );
CSingleton& operator=( const CSingleton& rhs);
public:
static T& Instance() {
static T m_Instance;
return m_Instance;
}
};

An example class which should become a "singleton" would for example look
like this:

class CObj {
protected:
CObj() {};
CObj& operator=( const CObj& rhs);
CObj( const CObj& rhs );

public:
~CObj() {};
friend class CSingleton<CObj>;
int m_i;
};

The usage would be like this:

CObj& MyObj = CSingleton<CObj>::Instance();

HTH
Chris
Jul 19 '05 #3
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<bd**********@sunnews.cern.ch>...
"Anthony Lansbergen" <an*****@EdAsys.nl> wrote in message
news:3f***********************@news.xs4all.nl...
Hi,
I want to create an abstract base class for singletons.
The base class should demand that the derived classes implement a static
instance member operation.
Is it possible to create a pure virtual static member function in a base
classe?
If not, is there an better elegant solution for this problem?


I'd recommend to use a templated wrapper for your singletons. Look at the
approaches in Modern Design C++ [snip]


Or download said approaches from
http://www.awl.com/cseng/titles/0-201-70431-5, though they very well
may not make total sense without reading the book first. I can't
remember if it uses template partial specalization, but if so, you'll
also neeed a really recent compiler to use.
Jul 19 '05 #4
"Anthony Lansbergen" <an*****@EdAsys.nl> wrote in news:3f015d4a$0$49099
$e*******@news.xs4all.nl:
Hi,
I want to create an abstract base class for singletons.
The base class should demand that the derived classes implement a static
instance member operation.
Is it possible to create a pure virtual static member function in a base
classe?
If not, is there an better elegant solution for this problem?


You can't have a virtual static member. The member will be static to the
base class, always. This caused me quite a few headaches until I realised
what was going on; in the end, I had to use templates to get round it.

Chris
--
Chris Whitworth * On XBL as parm * Playing Ghost Recon, MechAssault, MotoGP

"back when I was young, we had to travel back in time to put the tape
in so the game would load before we died."
--otama, ua2

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
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 ...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
7
by: Jerry Nixon | last post by:
I hope this is easy, First, I simply must not override MyMethod as shown in the sample code - that is an unbreakable requirement. The purpose of the event is to allow me to extend MyMethod...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
7
by: Rob Meade | last post by:
Hi all, Ok - I'm going around in circles and would appreciate some help. I have written a windows service, which up until I tried to be clever was working well :o) I have a main class which...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...
0
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...

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.