Solved

static class declaration

Posted on 2003-11-14
5
208 Views
Last Modified: 2010-04-01
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...........
0
Comment
Question by:schss
  • 2
5 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9746360
'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
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 9746453
In your code

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

you have a static pointer to a class instance of A. That means that if the pointer is valid, you have one instance of class A that can be accessed by all other instances of class A. For example if you store all instances to a linked list then pA might point to the head of the list, thus giving access to all other instances.

Regards, Alex
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 9767908
>> 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;
}

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249186
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
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question