-
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).
-
can you post the _full_ code of this class, and the other file that causes the error
-
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...
-
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.
-
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.
-
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?
-
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.
-
Sounds good Daved, thanks a bunch :0
-
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