Solved

static class declaration

Posted on 2003-11-14
5
206 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
Comment Utility
'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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now