Click here to Skip to main content
15,911,891 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Can I restrict a class not to have more than 2 objects? Pin
sunit56-Dec-05 20:52
sunit56-Dec-05 20:52 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
G Haranadh6-Dec-05 21:01
G Haranadh6-Dec-05 21:01 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
sunit56-Dec-05 21:09
sunit56-Dec-05 21:09 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
Cedric Moonen6-Dec-05 21:16
Cedric Moonen6-Dec-05 21:16 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
Cedric Moonen6-Dec-05 21:19
Cedric Moonen6-Dec-05 21:19 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
ThatsAlok6-Dec-05 21:50
ThatsAlok6-Dec-05 21:50 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
ThatsAlok6-Dec-05 22:10
ThatsAlok6-Dec-05 22:10 
AnswerRe: Can I restrict a class not to have more than 2 objects? Pin
Cedric Moonen6-Dec-05 21:14
Cedric Moonen6-Dec-05 21:14 
Here is another approach, a little bit more 'protected' than the one from sunit.

First, put your constructor and destructor private. This will prevent to create an instance of the class when it's not allowed. So for example you won't be able to do that:

CMyClass MyClass;
As the constructor is protected, you will have a compile error.
Then, to be able to get an instance of the class, declare a static member function (it must be static so it can be called without having an instance of the object). You need to have also a static counter that counts the number of instances (it must be static as it must be shared among all instances):

class CMyClass<br />
{<br />
public:<br />
   static CMyClass* GetNewInstance();<br />
<br />
protected:<br />
   CMyClass();<br />
   ~CMyClass();<br />
<br />
private:<br />
   static int m_iCounter;<br />
}


int CMyClass::m_iCounter = 0;<br />
CMyClass* CMyClass::GetNewInstance()<br />
{<br />
   if (m_iCounter<2)<br />
   {<br />
      m_iCounter++;<br />
      return new CMyClass;<br />
   }<br />
   return NULL;<br />
}


Now, the last thing to add is to be able to free the memory (and then decrement the counter when one of the object is freed). Add a public Destroy method:

void CMyClass::Destroy()<br />
{<br />
   m_iCounter--;<br />
   delete this;<br />
}


So, when you need a new instance of the object, call GetNewInstance:
CMyClass* NewClass = CMyClass::GetNewInstance();

It will return the new instance if succesfull or NULL if 2 instances already exists. Then, don't forget to free your object when not needed anymore:

NewClass->Destroy();

-- modified at 3:20 Wednesday 7th December, 2005
Sorry, I forgot to put my GetNewInstance function static Wink | ;)
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
Bob Stanneveld6-Dec-05 22:33
Bob Stanneveld6-Dec-05 22:33 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
toxcct6-Dec-05 22:50
toxcct6-Dec-05 22:50 
GeneralRe: Can I restrict a class not to have more than 2 objects? Pin
G Haranadh6-Dec-05 22:56
G Haranadh6-Dec-05 22:56 
QuestionRe: Can I restrict a class not to have more than 2 objects? Pin
David Crow7-Dec-05 3:08
David Crow7-Dec-05 3:08 
QuestionAlphaBlend + TextBox = crash?? Pin
dy136-Dec-05 20:24
dy136-Dec-05 20:24 
QuestionUrgent : About SHGetValue() API used for Registry Pin
Supriya Tonape6-Dec-05 20:03
Supriya Tonape6-Dec-05 20:03 
AnswerRe: Urgent : About SHGetValue() API used for Registry Pin
kakan6-Dec-05 20:18
professionalkakan6-Dec-05 20:18 
GeneralRe: Urgent : About SHGetValue() API used for Registry Pin
Supriya Tonape6-Dec-05 21:39
Supriya Tonape6-Dec-05 21:39 
GeneralRe: Urgent : About SHGetValue() API used for Registry Pin
kakan7-Dec-05 0:34
professionalkakan7-Dec-05 0:34 
GeneralRe: Urgent : About SHGetValue() API used for Registry Pin
Supriya Tonape7-Dec-05 1:06
Supriya Tonape7-Dec-05 1:06 
QuestionChanging Font Size in MS Word Automated Document Created using VC++(MFC) Pin
Sachinpatole6-Dec-05 19:56
Sachinpatole6-Dec-05 19:56 
QuestionThanks for help and need further..... Pin
usha_dolly6-Dec-05 19:35
usha_dolly6-Dec-05 19:35 
AnswerRe: Thanks for help and need further..... Pin
ThatsAlok6-Dec-05 21:55
ThatsAlok6-Dec-05 21:55 
QuestionPlease HELP Me( Porting from Win32 to MAC) Pin
vishix16-Dec-05 19:31
vishix16-Dec-05 19:31 
QuestionLittle Question Pin
Ankush Mehta6-Dec-05 19:08
Ankush Mehta6-Dec-05 19:08 
AnswerRe: Little Question Pin
S Douglas7-Dec-05 1:13
professionalS Douglas7-Dec-05 1:13 
GeneralRe: Little Question Pin
Ankush Mehta7-Dec-05 1:46
Ankush Mehta7-Dec-05 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.