Home > Net >  How to modify the same variable in different classes and modify it?
How to modify the same variable in different classes and modify it?

Time:01-23

I have many function definitions which I have placed in different cpp files with function declarations in their respective .h files.

I have a set of a variables which I have placed in a .h file. These variables need to modified by different functions. I am using static to keep the changes from each function, but I heard it is a bad coding practice. How else to do it ? For eg -

variables.h

class variable{
static int x;
static int y;
};

function1.h

class function(){
public:
void function1();
}

similar for function 2

function 1.cpp

void function1(){
// does something with x and y (used as (variable::x=2;variable::y=3)
}

function2.cpp

void function2(){
// does something with x and y (used as variable::x =2;variable::y =2)
}

main.cpp

int variable::x;
int variable::y;
int main(){

obj.function1(); (obj is object of function1 class)
obj2.function2(); (obj2 is object of function2 class)

cout << variable::x << variable::y << endl;
}

I was was using different objects in different cpp files but changes in one function were not reflecting in other. How it use it please help?

CodePudding user response:

You can simply move these variables into another class:

struct Shared {
    int x;
    int y;
};

Now you can pass an instance to this class as parameter to your function, this is called dependency injection:

void foo(Shared& shared) {
    shared.x = 4;
    shared.y = 2;
}

This is better because you don't have any global state anymore. You could use the function multiple times independent from each other by referencing a different instance of the Shared class.

It is very common to take this a step further by "injecting" the instance in the constructor of that class. This is helpful if the instance of that class should always reference the same instance:

struct Foo {
    Shared& m_shared;

    Foo(Shared& shared)
        : m_shared(shared)
    {

    }

    void foo() {
        m_shared.x = 4;
        m_shared.y = 2;
    }
};

CodePudding user response:

Yes, as you mentioned using static variable for this purpose is kind of anti-pattern. A better pattern (without knowing the background of the application) is using a composition pattern. If your functions f1() and f2() are in classes C1 and C2, you would e. g. create an additional data object D1 (with the variables in question), and inject and object of D1 in the constructor of C1 and C2, so both classes operation on a data object. There are also other solutions to this situation, but I guess thats the most general. Google for C Design Pattern to find more general patterns.

CodePudding user response:

You can use smart pointers for global objects

struct MyGlobal
{
    std::shared_ptr<Core> core;
    MyGlobal(){ core=std::make_shared<Core>(); }
    void changeVariableX(int X)
    {
        core->X = X;
    }
};

You can move, copy, do whatever you want with MyGlobal instances and they still point to same core item. Just make sure all of them are populated from same instance like this:

int main()
{
     MyGlobal global;

     auto something = useSomeFunctionWith(global);
     auto somethingElse = useAnotherFunctionWith(global);
     ...
     // use something and somethingElse to change X, both point to the same X
}

If functions will not be thread-safe then you should add a lock-guard into changeVariableX method.

  •  Tags:  
  • Related