Wednesday, April 11, 2012

extern static - C++ Forum

extern static - C++ Forum:


 extern static



vijirin (7) Aug 4, 2008 at 5:53am
Hi all,

Is it possible to use extern static. I have wrote a program where i used extern static. This was firing a compiler error. But the same i compiled in another compiler but it compiled successfully.

Can anyone please tell whether it is right to use extern static?
Is it possible to use static exter ?....

Thanks in advance 

anilpanicker (102) Aug 4, 2008 at 10:21am
storage type 'extern' means the variable declared in another file.
storage type 'static' means the value of the variable is static with respect to the scope of the variable. When the program reenter the scope you can retrieve the value. The scope can a function or a file or a class. For example if you define at the top of a fle
 
static int i=9;

every time you enter that file you can retrieve i=9. It is like global

now if you want to use these together, the you need declare seperately
For example:
In h1.h file you declare
 
extern int x; // this means actual x is defined somewhere else 

Then in h2.h file you declare
1
2
3
#include "h1.h"
static int x=25; // this means whereever we include h2.h the value for x 
// can be retrieved 


aEx.cpp
1
2
3
4
5
6
7
8
9
10
#include "h2.h"
#include 
using std::cout;

int display(){

	cout<<"x="<"\n";
	x++;
	return 0;
}


main.cpp
1
2
3
4
5
6
7
int main(){

	display();
	display();
	display();
	return 0;
}


'via Blog this'

No comments:

Post a Comment