inheriting a singleton?

Started by
2 comments, last by EvilCrap 22 years, 2 months ago
i would like to have a singleton base class with virtual functions that can be overridden, however, child classes create their own instances of the (static) singleton.... is there anyway to have a single instance of the singleton that can be interacted with virtually through derived classes?
Advertisement
Not through virtual funtions... if you made the function non-virtual, and didn''t override them, you could have them all ignore thier this pointer, and deference a static this pointer that''s part of the singleton class... not recommended.

What are you trying to accomplish?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by EvilCrap

i would like to have a singleton base class with virtual functions that can be overridden, however, child classes create their own instances of the (static) singleton....


Creating multiple instances of a singleton makes no sense.

A singleton a class that you can never instantiate more than once in the entire program, plus that instance lasts for the entire program. It is intended to represent things such as a program object.

If you derive classes from a singleton base class, you can only ever have one instance of a derived class or one instance of the base class, since instantiating a derive class also instantiates the base class.

If all you want is to prevent the base class from being instantiated by itself, then forget about singletons and just make the virtual functions pure virtual. Or make the constructors, destructors, and copy functions protected.

Good Luck!

Edited by - null_pointer on February 16, 2002 2:06:14 PM
Sounds like you want to have a base class with only pure virtual methods (some say an interface), then derive X different classes from it and make _those_ singleton. The singletons are then placed in some registry, then clients access the registry and call the virtual functions for each singleton through the base class interface. Yes?

This topic is closed to new replies.

Advertisement