Link to home
Start Free TrialLog in
Avatar of schss
schss

asked on

static class declaration

Suppose we have  class declaration :

class A
{
......................
.....................
private:
           static class A *pA;
};

My question is what is the use of the 'pA' variable and why is it defined static ?

Thanks...........
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

'static' means that his member exists once for all instances of the class, it is therefore a member of the class and not of an instance.

You may use this feature for counters or pointers to another class where only one instance exists, e. g.
static CMyApp* m_pApp;
         
Regards, Alex
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> My question is what is the use of the 'pA' variable and why is it defined static ?

so that it can be accessed even without instantiating an object of class A. of course, to dereference pA you will have to instantiate an object.

One possible use is to make it point to the latest instantiation of A. like:
A::A(){
  pA = this;
}
A::~A(){
  if(this == pA) pA=NULL;
}

Avatar of tinchos
tinchos

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: itsmeandnobodyelse {http:#9746453}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer