博客
关于我
Qt C++定义全局变量的两种方式
阅读量:544 次
发布时间:2019-03-09

本文共 583 字,大约阅读时间需要 1 分钟。

使用extern关键字

cglobal.h

#ifndef CGLOBAL_H
#define CGLOBAL_H
extern int testValue;
#endif // CGLOBAL_H

cglobal.cpp

#include "cglobal.h"
int testValue = 1;

调用方式

#include "cglobal.h"
qDebug() << testValue; testValue = 2;

使用static关键字

cglobal.h

#ifndef CGLOBAL_H
#define CGLOBAL_H
class CGlobal {
public:
CGlobal();
~CGlobal();
public:
static int testValue;
};
#endif // CGLOBAL_H

cglobal.cpp

#include "cglobal.h"
CGlobal::CGlobal() {}
CGlobal::~CGlobal() {}
int CGlobal::testValue = 1;

调用方式

#include "cglobal.h"
qDebug() << CGlobal::testValue; CGlobal::testValue = 2;

建议使用第二种方式

转载地址:http://bbysz.baihongyu.com/

你可能感兴趣的文章
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>
node模块的本质
查看>>
node环境下使用import引入外部文件出错
查看>>
node环境:Error listen EADDRINUSE :::3000
查看>>
Node的Web应用框架Express的简介与搭建HelloWorld
查看>>
Node第一天
查看>>
node编译程序内存溢出
查看>>
Node读取并输出txt文件内容
查看>>
node防xss攻击插件
查看>>
noi 1996 登山
查看>>
noi 7827 质数的和与积
查看>>
NOI-1.3-11-计算浮点数相除的余数
查看>>
noi.ac #36 模拟
查看>>
NOI2010 海拔(平面图最大流)
查看>>
NOIp2005 过河
查看>>
NOIP2011T1 数字反转
查看>>
NOIP2014 提高组 Day2——寻找道路
查看>>
noip借教室 题解
查看>>
NOIP模拟测试19
查看>>