[C++] 实时获取其他类的成员变量

[C++] 实时获取其他类的成员变量,第1张

[C++] 实时获取其他类的成员变量

调用其他类的成员变量,有两种方式:

1、将变量设置为全局变量,头文件中用extren修饰

test.h

extern int num;

test.cpp

int num;

Test::Test()
{
    num = 55;
}

getnum.cpp

#include "test.h"

void GetNum::modifyNum()
{
    qDebug() << num;
}

2、在类中将变量用static修饰,通过 “类名::变量名” 调用

test.h

class Test
{
    Q_OBJECT
public:
    Test();
    ~Test();
    static int num;

private:
};

test.cpp

int Test::num;

Test::Test()
{
    num = 55;
}

getnum.cpp

void GetNum::modifyNum()
{
    qDebug() << Test::num;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5692179.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存