当前位置:网站首页>Declared as a global variable
Declared as a global variable
2022-04-23 06:44:00 【The old man is outside the door】
Declare as a global variable
C++ Global variables should be defined in .cpp The document defines , Not in .h In file Definition , When it's defined , Can be in .h Use... In documents extern Keyword to declare . If in .h As defined in the document , Multi tier inclusion may cause repeated definition errors . Here's an example :
stay g_bash.cpp Define global variables in :
// g_bash.cpp
#include “g_base.h”
int g_TicketCount = 0; // Define global variables and initialize
stay g_bash.h Use... In documents extern Keyword to declare :
// g_bash.h
#ifndef G_BASE_H
#define G_BASE_H
extern int g_TicketCount; // Global variable declaration
#endif // G_BASE_H
Then when other files want to use these variables , as long as #include "g_base.h" That's all right. , And it will not cause repeated definition errors .main.cpp The following is an example :
#include
#include
#include g_base.h
extern int g_TicketCount; // Global variable declaration
using namespace std;
void proc()
{
cout<< proc(): <<endl;
g_TicketCount++;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout<< g_TicketCount = << g_TicketCount << endl;
proc();
cout<< g_TicketCount = << g_TicketCount << endl;
return a.exec();
}
https://www.jb51.net/article/224822.htm
版权声明
本文为[The old man is outside the door]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548038666.html
边栏推荐
猜你喜欢
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
【UDS统一诊断服务】一、诊断概述(1)— 诊断概述
[UDS] unified diagnostic service (UDS)
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
死区时间的分析与设置
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
C语言的浪漫
CUDA环境安装
[UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture
进程管理命令
随机推荐
LaTeX配置与使用
Matching between class template with default template argument and template parameter
四元数乘法
Shell脚本 &&和||的使用
卷积神经网络实现CIFAR100数据集分类
对象数组与对象指针
进程管理命令
CUDA环境安装
Shell脚本的通配符和特殊符号
gcc ,g++,gdb的安装
Generate random number
在MFC中使用printf
约瑟夫序列 线段树 O(nlogn)
Collection of practical tips for C language (continuously updated)
进程间通信-互斥锁
【UDS统一诊断服务】四、诊断典型服务(5)— 功能/元件测试功能单元(例行程序功能单元0x31)
类和对象
赛氪-二进制
Shell脚本 单引号、双引号和反引号的区别
[UDS] unified diagnostic service (UDS)