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)
|