博客
关于我
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/

你可能感兴趣的文章
python 实现字符串转整型
查看>>
Python 实现定时任务的八种方案!
查看>>
python 实现将RGBA 转换为RGB
查看>>
python字符串与url编码的转换
查看>>
Python 实现自动化测试 dubbo 协议接口
查看>>
python编程基础之十六
查看>>
python字符串input输入_Python input()函数:获取用户输入的字符串
查看>>
python 对json数据读取及保存与读取,对dump,dumps,load,loads的理解
查看>>
Python 对mysql数据库的操作
查看>>
Python 对象数据库列表
查看>>
Python 导入requests报错No module named requests
查看>>
Python 将JPEG图片批量改成jpg并删除JPEG图片
查看>>
python 将函数加到列表中进行调用
查看>>
python 将图片与字符串相互转换
查看>>
Python 嵌套字典全面指南
查看>>
Python 工具v简介
查看>>
Python编程入门指南:从零开始探索编程的奇妙世界
查看>>
python 常见内置函数setattr、getattr、delattr、setitem、getitem、delitem
查看>>
Python 常见的错误类型和继承关系
查看>>
python 常量_零基础学Python系列之一:Python的变量与常量
查看>>