TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: FRANK ADAM
from: KURT KUZBA
date: 1997-11-25 05:00:00
subject: awareness ?

FA>   Is it possible to make a class aware of another instance
FA>   of itself ? If it is, how would that be done ?
   A class may contain a static variable.
   If your constructor increments that variable, and your
   destructor decrements it, you will always know how many
   instances of that class are currently extant.
   Have a look at this once...
#include 
class AmIAlone
{
public:
   AmIAlone();
   ~AmIAlone();
   int HowMany(void) { return iHowMany; }
private:
   static int iHowMany;
};
AmIAlone::AmIAlone()
{
   iHowMany++;
}
AmIAlone::~AmIAlone()
{
   iHowMany--;
}
int AmIAlone::iHowMany = 0;
int main(void)
{
   char a;
   AmIAlone *One = new AmIAlone;
   cout HowMany() << '\n' << flush;
   AmIAlone *Two = new AmIAlone;
   cout HowMany() << '\n' << flush;
   AmIAlone *Three = new AmIAlone;
   cout HowMany() << '\n' << flush;
   delete Two;
   cout HowMany() << '\n' << flush;
   delete Three;
   cout HowMany() << '\n' << flush;
   delete One;
   cin >> a;
   return 0;
}
> ] Nothing is absolute, save Grace, Harmony, and Balance.......
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.