-
June 22nd, 2007, 04:54 AM
#1
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"
}
}
-
June 22nd, 2007, 06:13 AM
#2
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.
-
June 22nd, 2007, 06:23 AM
#3
Re: Singleton Correct Usage
-
June 22nd, 2007, 06:45 AM
#4
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;
}
-
June 22nd, 2007, 06:54 AM
#5
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;
-
June 22nd, 2007, 07:02 AM
#6
Re: Singleton Correct Usage
Thank you, that nailed it.
Just wondering, is there no way to set a static variable within the class itself?
-
June 22nd, 2007, 07:35 AM
#7
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
-
June 22nd, 2007, 07:51 AM
#8
Re: Singleton Correct Usage
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
-
June 22nd, 2007, 10:07 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|