|  | Singleton classes and inheritance |  | |
| | | Rune Allnor |  |
| Posted: Wed Jun 25, 2008 11:20 am Post subject: Singleton classes and inheritance |  |
| |  | |
Hi all.
I'm trying to use a singleton class as base class for a class hierarchy. Basically, I want to implement a factory pattern with singleton factories. However, I would like to use one base class which specifies the behaviour of the factory, and then use inheritance to derive more application-specific factory classes.
This approach causes all sorts of problems, mainly because the constructors and destructors of the singletons don't seem to be called (or rather, I don't understand where and when they are called). Consequently, I don't know where initializations should be done, and I try to do what I would normally do in the constructor in the ...::instance() method.
Of course this fails. Furthermore, I am unable to specify virtual helper functions since these seem to clash with the 'static' keyword which is essential to the singleton class.
Which makes me wonder: Am I chasing red herrings? Is it at all possible to unite singleton classes and inheritance?
Rune
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Bart van Ingen Schenau |  |
| Posted: Thu Jun 26, 2008 12:30 am Post subject: Re: Singleton classes and inheritance |  |
| |  | |
Rune Allnor wrote:
| Quote: | Hi all.
I'm trying to use a singleton class as base class for a class hierarchy. Basically, I want to implement a factory pattern with singleton factories. However, I would like to use one base class which specifies the behaviour of the factory, and then use inheritance to derive more application-specific factory classes.
|
As it is possible to have multiple singleton classes in an application, it is not fully clear to me which (base-)classes in your design can have only one instance and which classes can exist beside each other. Can you give a short explanation of how you are seeing this?
| Quote: | This approach causes all sorts of problems, mainly because the constructors and destructors of the singletons don't seem to be called (or rather, I don't understand where and when they are called).
|
The when depends a bit on how you implemented the singleton, but the constructor is usually called right at the moment that you create the first instance. Depending on the implementation of the singleton, the destructor may not be called at all (the object is never destroyed) or it is called during the closing of the application (after main() has returned).
| Quote: | Consequently, I don't know where initializations should be done, and I try to do what I would normally do in the constructor in the ...::instance() method.
Of course this fails. Furthermore, I am unable to specify virtual helper functions since these seem to clash with the 'static' keyword which is essential to the singleton class.
|
Using virtual helper functions during construction is asking for problems anyway. Only after you have reached the constructor body of the final class will you be able to call the 'expected' virtual functions.
| Quote: | Which makes me wonder: Am I chasing red herrings? Is it at all possible to unite singleton classes and inheritance?
|
It is definitely possible to use a singleton with inheritance, but depending of what your usage looks like, there can be a few hidden snags.
Can you show what you have tried so far? It is far easier to give advice once you see where someone has taken the wrong turn.
| Quote: | Rune
Bart v Ingen Schenau |
-- a.c.l.l.c-c++ FAQ: LINK c.l.c FAQ: LINK c.l.c++ FAQ: LINK
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|