?
Solved

static class declaration

Posted on 2003-11-14
5
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

741 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