Singleton Problems..

This is a discussion on Singleton Problems.. within the C++ Programming forums, part of the General Programming Boards category; Hey all I have recently made a Singleton class directly from a book I recently purchased, Here it is: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    102

    Smile Singleton Problems..

    Hey all I have recently made a Singleton class directly from a book I recently purchased, Here it is:

    Code:
    class Singleton
    {
    public:
    	 static void Create();
    	 static void Destroy();
    	 
    	 static Singleton * GetInstance()
    	 {
     	   		return s_pInstance;
    	 }
    	 
    	 static int GetNum();
    	   		 
    protected:
    		static Singleton* s_pInstance;
    		
    		//Hidden Constructor
    		Singleton();
    };
    
    // Implementation
    Singleton* Singleton::s_pInstance = NULL;
    
    void Singleton::Create()
    {
     	   if(!s_pInstance)
    		   s_pInstance = new Singleton;
    		
    		   
    }
    
    void Singleton::Destroy()
    {
     	   delete s_pInstance;
     	   s_pInstance = NULL;
     	   
    }
    However if I inherit from it with another class, I am not able to call any of it's own member functions, because of the Singleton Pointer.
    My Book doesn't give much information on it, and I might be able to find a way, but I don't want to make many changes without being sure there isn't a better way.
    Does any one have a suggestions?
    PS: I'm a pretty new programmed, and I appreciate any help.

    I am currently using the Dev C++ Software Compiler. The error message returned is..
    Class Singleton has no member function named (Function).
    Last edited by JJFMJR; 04-09-2007 at 06:05 PM. Reason: Additional Information

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you post the _full_ code of this class, and the other file that causes the error

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Maybe I'm nuts but I have the strangest feeling that you're probably using the (.) operator where you should be using the (->) operator. Maybe it's just the way the error is phrased, but I couldn't help but think this...
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Alrighty Guys this is the entire code being used:
    Code:
    /***********************************
    //** C++ Common Patterns
    ** Including the Singleton, Facade
    ** Visitor, and Observer
    ***********************************/
    
    #define NULL 0
    
    class Singleton
    {
    public:
    	 static void Create();
    	 static void Destroy();
    	 
    	 static Singleton * GetInstance()
    	 {
     	   		return s_pInstance;
    	 }
    	   		 
    protected:
    		static Singleton* s_pInstance;
    		
    		//Hidden Constructor
    		Singleton();
    };
    
    // Implementation
    Singleton* Singleton::s_pInstance = NULL;
    
    void Singleton::Create()
    {
     	   if(!s_pInstance)
    		   s_pInstance = new Singleton;
    		
    		   
    }
    
    void Singleton::Destroy()
    {
     	   delete s_pInstance;
     	   s_pInstance = NULL;
     	   
    }
    
    Singleton::Singleton()
    {}
    That's for the Singleton, here's the test.
    Code:
    /**************************
    *** Singleton Test
    ***************************/
    
    #include "Singleton.cpp"
    #include <iostrEam>
    #include <string>
    
    using std::cout;
    using std::cin;
    using std::string;
    
    class GameEngine : public Singleton
    {
    public:
    	   void Display();
    	   
    private:
    
    };
    
    void GameEngine::Display()
    {
     	 cout << "Works Fine";
    }
    
    int main()
    {
     	GameEngine::Create();
     	
     	GameEngine::GetInstance()->Display();
     	
     	string exit;
     	
     	cin >> exit;
     	
     	GameEngine::Destroy();
     	
    }
    That's everything, I get the error compiling the test.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The problem is that you aren't using inheritance properly. Create() creates an instance of Singleton, not GameEngine. Also, Display() is not a method in Singleton, so it cannot be called from the pointer returned by GetInstance().

    Off the top of my head I would think that it doesn't make sense to derive GameEngine from Singleton in this way, but I'm not sure at the moment what the normal way to handle this situation is.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Yes I'm aware thanks, this is just a test to make sure it would work with other classes I need in the future.
    However, what may I do to change the Instance, I've tried casting the pointer, (Templating, but not very well). Plus overiding the create method with a GameEngine::Create, but that didn't work well. What do you suggest to allow it to work properly?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What you're trying to do is make it easy to turn a regular class into a Singleton, right? Because technically, you could just do the same thing to GameEngine that you did to Singleton and get rid of Singleton entirely. Then GameEngine would be a singleton on its own.

    If you want a quick way to make something a singleton, then I'm sure there are examples out there, most likely using templates. Something tells me Alexandrescu's Loki library has an example, and boost is always a good place to search for this stuff. You probably don't even need to use the libraries, you can just get the basic principles from their code and change it to suit your needs.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Sounds good Daved, thanks a bunch :0

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    hey JJFMJR,

    singleton ("highlander") pattern can be a wide field. I suggest you read here a bit while having a glass of good red wine:

    http://www.codeproject.com/cpp/singleton.asp

    If thread safety or inheritance isn't required, the standard meyers* singleton will perform quite well. It may or may not outperform your approach because destruction is done automatically while application shutdown instead through an explicit call.


    *) http://www.aristeia.com
    Last edited by pheres; 04-11-2007 at 06:20 PM.

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. How To Derive from a singleton
    By appleGuy in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2007, 02:55 PM
  3. Thread-safe singleton.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 11:45 AM
  4. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 04:05 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21