Skip to main content
gamedev.net
🔒 Locked

Restring Classes

Started by The Lion King May 14, 2004 at 1:18 AM 4 replies 350+ views
Original Post
The Lion King
The Lion King
Hi, I am making a class which can only be declared and used once. And if the user used it else where then the application starts giving complicated errors since the class is accessing and initializing some confidential data which is not even in the class as a private member. My question is ... How do I restrict the user so that he/she can only declare and use the class once ... and if they declare it or use it again ... they are not allowed to do that by either giving an error message or just passing the same class that has been declared before. Hope I was a bit clear here :! Any suggestions or ideas? I can survive anything ... even NUKES!!! The Lion King
djsteffey
djsteffey
look into using Singleton

"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA
The Lion King
The Lion King
quote:
Original post by ncsu121978
look into using Singleton

"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA


Thanks ... this is going to help me a lot ... I have never used them though ... but lof of info on it in the forums ...

Thanks again

The Lion King
The Lion King
After having some sneak peak in the forums ... I did create an example ... but I am having a little trouble here ... here is the code ...

Main.h

#include <windows.h>

class cTest
{
public:
cTest ();
~cTest ();
static cTest* GetTest ();
protected:
cTest (const cTest&);
};


Main.cpp

#include "Main.h"
#define MsgBox(String) MessageBox (NULL, String, "Message", MB_OK);

cTest::cTest ()
{
MsgBox ("Constructor");
}

cTest::~cTest ()
{
MsgBox ("De-Constructor");
}

cTest* cTest::GetTest ()
{
MsgBox ("GetText");
static cTest TestInstance;
return &TestInstance
}

int WINAPI WinMain (HINSTANCE ThisInstance,
HINSTANCE,
LPSTR,
int)
{
cTest* Test1 = cTest::GetTest ();
cTest Test2;

return 0;
}


I think I am missing a few pieces here and there ... here are my questions ...

- Is this the complete singleton implementation ?
- I can use

quote:
cTest* Test1 = cTest::GetTest ();


and

quote:
cTest Test2;


both.

Is there anyway I can restrict the user to declare only cTest* Test1 = cTest::GetTest ();

Any help would be appreciated

[edited by - The Lion King on May 14, 2004 12:48:25 AM]
The Lion King
The Lion King
quote:
Original post by Anonymous Poster
There should not be a public constructor declared if you want to only be able to access a instance of the class using a getInstance() method. You still have to declare a constructor, just make it private. If you don''t declare it private a default public constructor will be created.

I did make them private in the first place ... but the compiler gave an error

Topic Locked

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

Sign in to reply to this topic.