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

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

使用extern关键字

cglobal.h

#ifndef CGLOBAL_H#define CGLOBAL_Hextern 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_Hclass 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/

你可能感兴趣的文章
NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
查看>>
Nuxt Time 使用指南
查看>>
NuxtJS 接口转发详解:Nitro 的用法与注意事项
查看>>
NVelocity标签使用详解
查看>>
NVelocity标签设置缓存的解决方案
查看>>
Nvidia Cudatoolkit 与 Conda Cudatoolkit
查看>>
NVIDIA GPU 的状态信息输出,由 `nvidia-smi` 命令生成
查看>>
NVIDIA-cuda-cudnn下载地址
查看>>
nvidia-htop 使用教程
查看>>
nvidia-smi 参数详解
查看>>
Nvidia驱动失效,采用官方的方法重装更快
查看>>
nvmw安装node-v4.0.0之后版本的临时解决办法
查看>>
nvm切换node版本
查看>>
nvm安装以后,node -v npm 等命令提示不是内部或外部命令 node多版本控制管理 node多版本随意切换
查看>>
ny540 奇怪的排序 简单题
查看>>
NYOJ 1066 CO-PRIME(数论)
查看>>
NYOJ 737:石子合并(一)(区间dp)
查看>>
nyoj------203三国志
查看>>
nyoj58 最少步数
查看>>
OAuth 及 移动端鉴权调研
查看>>