Archived

This topic is now archived and is closed to further replies.

inheriting a singleton?

This topic is 5784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites