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

你可能感兴趣的文章
Object.keys()的详解和用法
查看>>
objectForKey与valueForKey在NSDictionary中的差异
查看>>
Objective - C 小谈:消息机制的原理与使用
查看>>
OBJECTIVE C (XCODE) 绘图功能简介(转载)
查看>>
Objective-C ---JSON 解析 和 KVC
查看>>
Objective-C 编码规范
查看>>
Objective-Cfor循环实现Factorial阶乘算法 (附完整源码)
查看>>
Objective-C——判断对象等同性
查看>>
objective-c中的内存管理
查看>>
Objective-C之成魔之路【7-类、对象和方法】
查看>>
Objective-C享元模式(Flyweight)
查看>>