一、使用extern关键字
cglobal.h
#ifndef CGLOBAL_H
#define CGLOBAL_H
extern int testValue;
#endif // CGLOBAL_H
cglobal.cpp
#include "cglobal.h"
int testValue=1;
调用方式
#include "cglobal.h"
#include <QDebug>
qDebug()<<testValue;
testValue=2;
qDebug()<<testValue;
二、使用static关键字
cglobal.h
#ifndef CGLOBAL_H
#define CGLOBAL_H
class 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>
qDebug()<<CGlobal::testValue;
CGlobal::testValue=2;
qDebug()<<CGlobal::testValue;
建议使用第二种方式