Thread: How To Derive from a singleton

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    How To Derive from a singleton

    Hi,

    Ive got a texture manager class. Its a singleton as I want it to manage all my different texture objects. Its also an abstract class as there are multiple implementations of textures (such as bmp and tga loading etc) that i want to control using a base ptr.

    What i want is to derive different classes such as a tgaTexture bmpTexture etc, they dont want to be singletons, as there can be multiple instances at any one time.

    My problem is that I can't by the nature of the singleton pattern that I know derive classes from this singleton. Because my constructor of my singleton is private and if it wasnt I would be creating multiple instances of a class that i want lmited to one instance.

    Is there any way of having a singleton and aslo deriving from that singleton?

    Many Thanks
    Alex

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is a texture manager a texture?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Because my constructor of my singleton is private and if it wasnt I would be creating multiple instances of a class that i want lmited to one instance.
    An object of the child class will also be an object of the singleton class. That's set in stone by how inheritance works. You can make your constructor protected and the child's constructor public and then only the child objects can call the singleton constructor. That still breaks the rules of the singleton, but you're not going to be able to use inheritance if you don't break a rule somewhere.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Noir
    An object of the child class will also be an object of the singleton class. That's set in stone by how inheritance works. You can make your constructor protected and the child's constructor public and then only the child objects can call the singleton constructor. That still breaks the rules of the singleton, but you're not going to be able to use inheritance if you don't break a rule somewhere.
    offtopic: glad that you're back Noir.

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Really? I got the strong vibe that nobody liked me here. But I'm stubborn.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I got the strong vibe that nobody liked me here.
    Nah, welcome back.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by laserlight
    Is a texture manager a texture?
    no, it keeps tabs on current textures and what they are binded to etc. It also provides generic loading but it is never a texture. Possibly it should no be derived from....hmmm

    What could I do????

    -Alex

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    no, it keeps tabs on current textures and what they are binded to etc. It also provides generic loading but it is never a texture
    In that case, why do you use (presumably) public inheritance? You could have a different abstract base class from which tgaTexture bmpTexture etc could be derived, and then use composition with respect to the texture manager.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by laserlight
    In that case, why do you use (presumably) public inheritance? You could have a different abstract base class from which tgaTexture bmpTexture etc could be derived, and then use composition with respect to the texture manager.
    Hi,

    Thanks, that sounds like the way to go.

    I should really know this, forgive me for my moment of stupidity...


    Cheers
    Alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singleton template problem
    By cloudy in forum C++ Programming
    Replies: 14
    Last Post: 01-11-2009, 05:40 AM
  2. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM
  3. Thread-safe singleton.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 10:45 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 06:16 PM