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

本文共 570 字,大约阅读时间需要 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"#include 
qDebug()<
<

二、使用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"#include 
qDebug()<
<

建议使用第二种方式

原创不易,转载请标明出处:

 

你可能感兴趣的文章
MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法
查看>>
MySQL之SQL语句优化步骤
查看>>
MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
查看>>
Mysql之主从复制
查看>>