通过参数传递变量并稍后在 class 中修改它

Passing a variable through parameters and modifying it later in the class

如何在 class 中存储一个变量,然后在程序流程中修改它?

例如:

bool bGlobalStatus = false;

class foo
{
public:

    foo(){}
    ~foo(){}

    void InitGlobalBool(bool &var)
    {

        // I can change the value here, but I want to store the variable and modify it later.
        // var = !var;
    }

    void Update()
    {
        // this will be called in program flow ...
        var = !var; // this variable should be in my case bGlobalStatus, depends which variable I put inside the InitGlobalBool function 
    }
};

int main()
{
   foo Foo;
   Foo.InitGlobalBool(bGlobalStatus); 
   return 0;
}

我想你想做这样的事情:

bool bGlobalStatus = false;

class foo
{
public:
    bool *mVar{};
    foo(){}
    ~foo(){}

    void InitGlobalBool(bool &var)
    {

        mVar = &var;
    }

    void Update()
    {
        *mVar = !(*mVar);
    }
};

int main()
{
   foo Foo;
   Foo.InitGlobalBool(bGlobalStatus); 
   return 0;
}