首页 > 编程 > C++ > 正文

C++全局变量操作

2019-11-11 05:53:47
字体:
来源:转载
供稿:网友
全局变量操作主要有两种:一、重新建立一个类。首先,我们建立一个.h和一个.cpp文件。在.h中定义一个类,类里面定义public成员,全部为static。然后在.cpp文件中,对这些变量进行初始化。.h文件如下#ifndef GLOBAL_H#define GLOBAL_Hclass Global// This is available in all editors.{public:static bool g_loginFlag;static QString g_path;static QString g_name;static bool g_isPlay;static int g_delayTime;static int g_loopTime;};#endif // GLOBAL_H.cpp文件如下#include "global.h"bool Global::g_loginFlag=false;QString Global::g_path = "";QString Global::g_name = "";bool Global::g_isPlay = false;int Global::g_delayTime = 10;int Global::g_loopTime = 1;二、extern方法,不需要定义一个类,但是同样需要一个.h和一个.cpp文件.h文件#ifndef GOLBALOBJECT#define GOLBALOBJECTstatic RunConfig g_Config;extern RobotFileConfiguration g_robotFileConfiguration;extern RobotFileConfiguration g_sendIOConfiguration;#endif // GOLBALOBJECT.cpp文件#include "golbalobject.h"RobotFileConfiguration g_robotFileConfiguration={0}; RobotFileConfiguration g_sendIOConfiguration ={0};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选