472,958 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 3742
"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:...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.