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

Singleton template and derived class

Hello,

I want to do derive a class from a Singleton template base class. Is
there a way to solve the following problem:

template<class T>
class CSingleton
{
public:
static T& GetInstance(void)
{
static T obj;
return obj;
}

protected:

virtual ~CSingleton() { };
CSingleton() {};

};

class App : public CSingleton<App>
{
protected:
App();
friend class CSingleton<App>; // otherwise c'tor cannot be
called
// is there a better way?
};

class App1 : public App
{
protected:
App1();
friend class CSingleton<App1>;
};

int main()
{
App1::GetInstance(); // App::GetInstance() is called
}
Is there a way to solve this? I do not like the friend deklaration and
I want to be able to create objects of type App1.

Thank you.

Christian Rousselle
Jul 22 '05 #1
2 3761
"Chr?stian Rousselle" <Ch*******@Rousselle.de> schrieb im Newsbeitrag
news:7b**************************@posting.google.c om...
Hello,
I want to do derive a class from a Singleton template base class. Is
there a way to solve the following problem:

template<class T>
class CSingleton
{
public:
static T& GetInstance(void)
{
static T obj;
return obj;
}

protected:

virtual ~CSingleton() { };
CSingleton() {};

};

class App : public CSingleton<App>
{
protected:
App();
friend class CSingleton<App>; // otherwise c'tor cannot be
called
// is there a better way?
};


I think there is no better way to derive the Singleton behaviour.

Some aspects: http://www.oop-trainer.de/Themen/Singleton.html in German ;-)

Ralf

www.oop-trainer.de

Jul 22 '05 #2

"Chr?stian Rousselle" <Ch*******@Rousselle.de> wrote in message
news:7b**************************@posting.google.c om...
Hello,

I want to do derive a class from a Singleton template base class. Is
there a way to solve the following problem:
Yes - don't even try this approach - it doesn't save you much
typing and what there is is simple and it
does the wrong thing.

template<class T>
class CSingleton
{
public:
static T& GetInstance(void)
{
static T obj;
return obj;
}

protected:

virtual ~CSingleton() { };
CSingleton() {};

};

class App : public CSingleton<App>
{
protected:
App();
friend class CSingleton<App>; // otherwise c'tor cannot be
called
// is there a better way?
};

class App1 : public App
{
protected:
App1();
friend class CSingleton<App1>;
Why?
App1 is not a CSingleton<App1> it is a CSingleton<App>
I doubt very much that this is what you intended.
};

int main()
{
App1::GetInstance(); // App::GetInstance() is called
And returns an App NOT an App1
}
Is there a way to solve this? I do not like the friend deklaration and
I want to be able to create objects of type App1.

Thank you.

Christian Rousselle


There is no foolproof way of deriving from singletons without dynamic
linking
( I java you would have the GetInstance method dynamically load a class
instance
given only the name as a string in the environment)

A method that can work is to have no templates and static methods in App as
follows:

class App
{
public:
App()
{
if( inst )
throw "Attempting to create second App";
inst = this;
}

virtual ~App()
{
// DO NOT delete inst (this is it)
}

static App* instance()
{
return inst;
}

// presumably virtual methods go here
private:
static App* inst;
};

// in App.cpp
App* app::inst = 0;

// in App1.h
class App1 : public App
{
public:
App1() {} // calls App ctor and hence App::inst points to this.
};

// In the file containing main

static App1 app; // or AppN app - the most derived one

I find this technique simple and way more useful when testing because you
can replace App
with a test version which is impossible with the function static approach.

P.S. Don't get too hung up on trying to make the compiler prohibit
everything -
The public ctor will not be a problem in practice.
Jul 22 '05 #3

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

Similar topics

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)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
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 ...
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
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++...
9
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would ¨like to connect different databases of the same...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
5
by: Markus Dehmann | last post by:
I need a Singleton for general program options so that all classes can access it. I use the code below (adapted from the Wikipedia singleton example). But the problem is if I change one variable...
2
by: puzzlecracker | last post by:
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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.