博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt C++定义全局变量的两种方式
阅读量:539 次
发布时间: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()<
<

建议使用第二种方式

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

 

你可能感兴趣的文章