C++全局变量操作

    xiaoxiao2021-03-26  30

    全局变量操作主要有 两种: 一、重新建立一个类。 首先,我们建立一个.h和一个.cpp文件。在.h中定义一个类,类里面定义public成员,全部为static。然后在.cpp文件中,对这些变量进行初始化。 .h文件如下 #ifndef GLOBAL_H #define GLOBAL_H class 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 GOLBALOBJECT static 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};
    转载请注明原文地址: https://ju.6miu.com/read-662830.html

    最新回复(0)