Declaring and using a "singleton"

hi

How can I use and declare a singleton? My issue is in using the singleton object it self, so far here what I've done:

//header file potion
1
2
3
4
5
6
7
8
9
10
11
12
13
class CStart_Menu :
	public CStates
{
private:
	CStart_Menu(void);

public:
.......
	//SINGLETON DECLARATION
	static CStart_Menu * Instance();
	
	virtual ~CStart_Menu(void);
};


.cpp file part
1
2
3
4
5
6
7
8
9
10
11
//SINGLETON DECLARATION
	 CStart_Menu* CStart_Menu::Instance()
	{
		
		//this is where the instace declare it self
		//it must be in a cpp (not header) file to stop 
		//multi declaration
		static CStart_Menu qStart_Menu_State;

		return &qStart_Menu_State;
	}
I am not quite understand what your issue is.

What I see is that you put the singleton object in a function(CStart_Menu::Instance()), it's only visible to this function, if other member functions want to access it, they have to call CStart_Menu::Instance() first to get the singleton's pointer, this is a bit inconvenient, you can introduce a pointer to this class to keep the singleton's address.
Topic archived. No new replies allowed.