CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2007
    Posts
    12

    Singleton Correct Usage

    Finding clear examples of singleton patterns isn't hard, but I am having trouble implementing them. Mainly, is there a way to set one at class level or am I completely off track?:

    class SingletonUser
    {
    protected:
    Singleton* singleton;

    Public:
    SingletonUser()
    {
    singleton = Singleton::GetInstance();
    // error LNK2001: unresolved external symbol
    // "private: static class Gravity * Gravity::m_instance"
    }
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Singleton Correct Usage

    You do not show your Singleton class, but I assume that in it you
    have something like:

    Code:
    class Gravity
    {
    // other stuff
    
    static Gravity* m_instance;	// declares a static variable
    };
    You declared a static class variable, but you still need to define it

    Code:
    Gravity* Gravity::m_instance= 0;
    Last edited by Philip Nicoletti; June 22nd, 2007 at 06:51 AM.

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Singleton Correct Usage

    [ Redirected thread ]

  4. #4
    Join Date
    Jun 2007
    Posts
    12

    Re: Singleton Correct Usage

    I don't understand. The pointer is set in the GetInstance() method (if it hasn't been already). Why do you set it to zero, and where?

    Here is my singleton:

    Code:
    // ***** Gravity.h *****
    class Gravity
    {
    
    public:
    
    	static Gravity* GetInstance();
    
    	~Gravity();
    
    	double GetGravity();
    	void SetGravity(double newVal);
    
    
    protected:
    	Gravity();
    	Gravity(const Gravity& prm1);
    	Gravity& operator=(const Gravity& prm1);
    
    private:
    	double m_gravity;
    
        static Gravity* m_instance; // Defined in the source file
    };
    
    // ***** Gravity.cpp *****
    
    Gravity::Gravity()
    : m_gravity(-9.81)
    {}
    
    Gravity::~Gravity(){}
    
    Gravity* Gravity::GetInstance()
    {
          if (!m_instance) 
    	  {
    		  static Gravity instance;//m_instance = new Gravity(); 
    		  m_instance = &instance;	  
    	  }
                      return m_instance; 
    }
    
    
    // Properties
    double Gravity::GetGravity(){
    
    	return m_gravity;
    }
    
    
    void Gravity::SetGravity(SurfaceGravity newVal){
    
    	m_gravity = newVal;
    }

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Singleton Correct Usage

    Any static class member variable must to defined.

    So in gravity.cpp ...

    Code:
    include "gravity.h"
    
    Gravity* Gravity::m_instance= 0;

  6. #6
    Join Date
    Jun 2007
    Posts
    12

    Re: Singleton Correct Usage

    Thank you, that nailed it.

    Just wondering, is there no way to set a static variable within the class itself?

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Singleton Correct Usage

    you can initialize it within the class itself only if it is of const integral
    or const enumeration type ... but it still must be defined.

    Code:
    struct S
    {
       static const int x = 10;
    };
    
    const int S::x; // still needed

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Singleton Correct Usage

    Quote Originally Posted by Philip Nicoletti
    you can initialize it within the class itself only if it is of const integral
    or const enumeration type ... but it still must be defined.

    Code:
    struct S
    {
       static const int x = 10;
    };
    
    const int S::x; // still needed
    While this is correct by specification, it may cause problems with certain compilers, if you try to take the address of "x".....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jun 2007
    Posts
    12

    Re: Singleton Correct Usage

    Thank you Philip and Wizard, that really clarified the correct implementation of static member variables for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured