简体   繁体   中英

static member variable file scope

Static variable has the scope inside that file only where they are been declared, as shown in below code:

file1-

static int a;

file2-

extern int a;

This will give linking error as static variable a has the scope in file1 only. But I am confused with below code:

file2-

#include "file1"
extern int a;

Here it would not give any linking error. Then it means compiler is refering "a" which is declared in file1. But when you debug you will find address of variable "a" is different in file1 and file2. Is the compiler creating a another global variable "a" in file2?

complete code-

file temp1.h -

static int w = 9;

class temp1 
{
public:
   temp1(void);
public:
   ~temp1(void);

   void func();

};

........................cpp.................

temp1::temp1(void)
{
   int e =w;
}

temp1::~temp1(void)
{
}
void temp1::func()
{
}

....................................... file2-

#include "temp1.h"

extern int w;
int _tmain(int argc, _TCHAR* argv[])
{
   w  = 12;

   temp1 obj;
   return 0;
}

here when i debug and check the value and adrress in temp1 constructor and in file2 is different.

static and extern are not to do with files , they're to do with translation units . Remember that when you #include something, the preprocessor is effectively doing a copy-and-paste. static , etc. refer to the result of that process, which is a translation unit.

So in your second example, there is only one translation unit. So there is only one variable a . [But note, doing it this way is considered very bad style.]

file1:

static int a;

file2:

extern int a;

There are two variables referenced here. The first is a declaration and definition of a variable with internal linkage in file1 ; the second is a declaration only of a variable with external linkage in file2 . This doesn't necessarily cause an error; it is completely legal to have a variable with external linkage used in some translation units and identically named variables with internal linkage used by other translation units. Any link error would only occur if a is used in file2 and there isn't a definition for this a in file2 or any other translation unit in the program.

#include "file1"
extern int a;

In this example you have combined the two files into a single translation unit. The variable a is first declared and defined in file with internal linkage due to static , the second is just a re-declaration which doesn't alter the previous linkage. The second declaration is redundant in this instance. If you are still compiling both files separately as well as including file1 from file2 then each translation unit ( file1 and file1 + file2 ) will have its own distinct variable called a .

Note that if you had used extern int a; followed by static int a; then this would be a compile error as the first declaration would declare a to have external linkage if no previously declaration was visible and then the second declaration and definition would cause an error because the linkage implied by static int a; would conflict with the previous declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM