由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - global variable usage question in C++
相关主题
Why should i include .cpp instead of .h怎样include一个函数
弱问c++里有没有NULL这个keyword?gcc 编译的时候要包括 header source file 吗?
又一个初级问题: C++中多如牛毛的#define格式is there any lib can read .Z file in c++/c program?
弱问C++一个问题 一直不解另一个Fortran 问题
问低级问题弱问一个ifdef 和ifndef
C 里面有办法永久改变一个指针的属性吗?c++,这种做法不行?
两个class的交叉引用问题问个两个.h文件互相include的问题
最基本的C语言编程问题请教forward declaration
相关话题的讨论汇总
话题: value话题: variable话题: head话题: function话题: define
进入Programming版参与讨论
1 (共1页)
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(){

1 (共1页)
进入Programming版参与讨论
相关主题
forward declaration问低级问题
Xerces-C++ in vs.net questionC 里面有办法永久改变一个指针的属性吗?
问个C++问题,高手帮帮忙两个class的交叉引用问题
【请教】mpicc 和 mpiCC编译问题最基本的C语言编程问题请教
Why should i include .cpp instead of .h怎样include一个函数
弱问c++里有没有NULL这个keyword?gcc 编译的时候要包括 header source file 吗?
又一个初级问题: C++中多如牛毛的#define格式is there any lib can read .Z file in c++/c program?
弱问C++一个问题 一直不解另一个Fortran 问题
相关话题的讨论汇总
话题: value话题: variable话题: head话题: function话题: define