Skip to main content
gamedev.net
🔒 Locked

Global pointer to static class

Started by Sir_Spritely Mar 2, 2004 at 3:48 PM 4 replies 250+ views
Original Post
Sir_Spritely
Sir_Spritely
I have two .cpp files (File1.cpp and File2.cpp) each containing a class, let''s say Class A and Class B. Now Class A in File1.cpp is the base class and Class B in File2.cpp is derived from it. In File1.cpp (Not part of Class A!) I have static ClassA* g_ClassAPointer = NULL. By my reckoning I am saying ClassA is static and I have the pointer variable ClassAPointer set to NULL. So ClassAPointer is a global variable with scope only for File1.cpp meaning File2.cpp cannot see ClassAPointer. Then in ClassA''s constructor I have g_ClassAPointer = this. So now g_ClassAPointer contains the memory address of File1.cpp thus ClassA (I''m lost here I think!!). I can then use g_ClassAPointer like, g_ClassAPointer->ClassA Member Function. So in essence I am hiding and protecting the global pointer g_ClassAPointer from Class B and File2.cpp. g_ClassAPointer''s reason for existence is to work witbin another global function inside File1.cpp. It is used like, return g_ClassAPointer->ClassA_Member_function. If anyone out there has just read this and can decipher it (it is English, honest), and can help out I would really appreciate it.
Palidine
Palidine
um, so what''s your question? you didn''t ask one

-me
Sir_Spritely
Sir_Spritely
Sorry,

I guess I am asking, is what I have written above correct? Can someone look it over and correct any mistakes in there. Also the part where I have written (I''m lost here I think), is this accurate what I have put prior to that?

Drath
Drath
To start with your classes should be in header files.
Just to correct something, your class is not static, your single pointer is.

In your constuction code you are setting your variable to point to the latest construction of your class. Very bad practice! Well if you want a single unique instance of a class as I would guess you refer to through yuour use of ''static'' then you should look into singletons. Google will help you there. There is a lot of documention on them around. You really should be avoiding global things altogether.
Through a singleton you can then access something through CLASS::GetSingleton()->Func();
Drath
Drath
And this post really belongs in the beginners section.
Sir_Spritely
Sir_Spritely
Sorry about posting in here, please accept my most humble apology

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.