Redian新闻
>
global variable usage question in C++
avatar
global variable usage question in C++# Programming - 葵花宝典
a*e
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
avatar
y*i
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(){

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。