Reputation: 218
I'm new to c++ and this was kinda confusing to me I was trying to understand some code source where it's doing something like
Using 3 static pointers to 3 classes allocating that 3 pointers from the main to create new objects to that 3 classes using them to access members of that instance of classes they created Why not just use instances ? So they create it on the stack and not the heap without the function scope ? it's already in main here is the code source
int _tmain(int argc, _TCHAR* argv[])
{
...
SetConsoleTextAttribute(hStdOut, FOREGROUND_CYAN);
printf("Creating Database... ");
CDatabaseRoot::Create();
SetConsoleTextAttribute(hStdOut, FOREGROUND_LIME);
WriteLn("Completed.");
SetConsoleTextAttribute(hStdOut, FOREGROUND_CYAN);
printf("Creating Servers... ");
CDatabaseRoot::AuthServer->Port = 9958;
CDatabaseRoot::AuthServer->OnClientConnect = AuthConnect;
at the deader of CDatabaseRoot
class CDatabaseRoot
{
public:
static CDatabaseRoot* Core;
static CServerSocket* AuthServer;
static CServerSocket* GameServer;
static void Create();
static void Destroy();
at the cpp
CDatabaseRoot* CDatabaseRoot::Core;
CServerSocket* CDatabaseRoot::AuthServer;
CServerSocket* CDatabaseRoot::GameServer;
void CDatabaseRoot::Create()
{
CDatabaseRoot::Core = new CDatabaseRoot();
CDatabaseRoot::AuthServer = new CServerSocket();
CDatabaseRoot::GameServer = new CServerSocket();
}
Why is it done this way? Is there a better way to get it done? I would like some explanation please and thanks
Upvotes: 1
Views: 2201
Reputation: 760
If the constructor checks to make sure only one instance of the object exists and returns it if it does instead of creating a new one it would be implementing the singleton pattern, but it's not. Just as the other answerer said, any instance of your class will share the same isntances of those static pointers. If those objects were instead methods, you could invoke the methods without creating an instance of the object first since they are static, yet that is another subject.
Upvotes: 1
Reputation: 6332
Static members mean that they're shared among all instances of the class. Since the class is holding what is presumably the sole instance of the CDatabaseRoot
, it looks like the code is implementing a singleton pattern (though it is not enforcing it by checking for null before creating, so calling Create()
multiple times without calling a corresponding Destroy()
could cause issues). Making the data members public is also generally considered bad form and a recipe for bugs.
There are several ways to implement singletons in C++. See Alexandrescu's Modern C++ Design for some variants (code available here), and see the Wikipedia article mentioned above for several others.
Upvotes: 2
Reputation: 3623
It appears this is being done so methods other than main can access these global services without them being passed in as arguments. Similar to the Singleton pattern but with no protection (what happens if "Create" is called twice?).
Which is a bad idea in the first place. So you are smart to look at that code and be a little skeptical.
I would propose instead defining a "context" structure (or class) which encapsulates these services:
struct Context
{
CDatabaseRoot* Core;
CServerSocket* AuthServer;
CServerSocket* GameServer;
};
You would initialize this in main, then pass it into any class or object which needs it.
Upvotes: 2