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...........
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...........
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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;
}
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;
}
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
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
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