a***e 发帖数: 1140 | 1 Three files: head.h, f1.cc, f2.cc
in head.h
#ifndef head_h_
#define head_h_
static int value;
void my_function();
#endif
in f1.cc
#include "head.h"
int main(){
value=3;
my_function();
cout<
}
in f2.cc
#include "head.h"
void my_function(){
value=5;
}
For succinct, I define the global variable in head file.
If I omit | y****i 发帖数: 156 | 2 static usually should be used in .c/cpp file. Its visibility is within the
file. You should not use in .h file. Otherwise you define the variable when
you include the .h file.
【在 a***e 的大作中提到】 : Three files: head.h, f1.cc, f2.cc : in head.h : #ifndef head_h_ : #define head_h_ : static int value; : void my_function(); : #endif : in f1.cc : #include "head.h" : int main(){
|
|