Archived

This topic is now archived and is closed to further replies.

Zorodius

Problem enforcing a singleton

Recommended Posts

I''m using C++. I''m trying to create a class that is a singleton, and can only be accessed by getting a pointer to the one instance by using a method of that class, called AcquireInstance. The singleton class is called textureManagerGL. I''ve declared the constructor, copy constructor, and assignment operators of the singleton class as private to try and enforce this. This works sometimes, but not always. If I try to write a statement like textureManagerGL myTM; then it causes a compiler error. However, if I declare an instance of textureManagerGL as a member of another class, I don''t get any errors. What am I doing wrong? Or is it just that my compiler isn''t enforcing the syntax the way it should?

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
just a hint... try instanciating the class containing it...


I am, it compiles and runs.

The thing is, the class that it''s a member of isn''t just any ol'' class. If I try to do the following:


class foo
{
public:
int a;
private:
textureManagerGL mytm;
};

... later, in some function...

foo * bar = new foo;


Then the compiler DOES give me an "illegal access to private/protected member" error, as it should.

However, in my actual code, the class where I''m declaring a member of type textureManagerGL is called splashState. splashState is a subclass of abstractState, and is dynamically allocated at runtime.

So - textureManagerGL as a member of foo == error when instantiated
textureManagerGL as a member of splashState, the dynamically allocated subclass == no error

How can I fix?

Share this post


Link to post
Share on other sites
You''re doing something wrong with your "splashState" then. Are you sure it''s a class member, rather than pointer-to-class? Pointer-to-class will work fine.


class MySingleton {
public:
static MySingleton * It();
private:
void * whatever;
MySingleton() { whatever = someStuff(); }
~MySingleton() { delete whatever; }
MySingleton( MySingleton const & ); // no body!
MySingleton & operator=( MySingleton const & ); // no body!
};

MySingleton *
MySingleton::It()
{
static MySingleton it;
return ⁢
}


That''s all there''s to it. You should not be able to create an instance of this class from a non-friend or non-member; if the compiler lets you, then it''s broken (or you''re picking up the name "MySingleton" from somewhere other than this declaration).

Try getting sizeof(MySingleton) in the code that (indirectly) creates it and see what the compiler thinks, as a possible debugging strategy.

Share this post


Link to post
Share on other sites
I''ve checked the sizeof, it seems to correctly reflect the size of an instance of the class.

My splashState declaration is:


class splashState : public abstractState
{
public:
splashState();
~splashState();

void Execute(void);
LRESULT CALLBACK React(HWND, UINT, WPARAM, LPARAM);
private:
textureManagerGL tmgl; // <=========== ??
void Render();
};


I''m dynamically allocating an instance of splashState in my driver file with the following:


abstractState::Change(new splashState);


The declaration of the singleton class is the following:


class textureManagerGL
{
public:
( ... blah blah blah ...)
~textureManagerGL();
protected:
static textureManagerGL * instance; // Static pointer to the singleton instance

static texture * textures;

bool LoadTexture(char * filename); // Internal loading routine


private:
// Private to prevent misuse of the singleton

textureManagerGL();
textureManagerGL(const textureManagerGL &);
const textureManagerGL& operator=(const textureManagerGL &);
};

Share this post


Link to post
Share on other sites
If anyone sees anything that I''m doing wrong that would explain why the code compiles with an instance of textureManagerGL as a member of splashState, I''d love to know. I''m fine with just writing this off as a compiler bug, but if it''s not, I''d like to know how to fix it. Thanks a lot

Share this post


Link to post
Share on other sites