Solved

static class declaration

Posted on 2003-11-14
5
212 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

691 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