Home > Net >  Is it possible to affect the same variable of two different objects of the same class in C ?
Is it possible to affect the same variable of two different objects of the same class in C ?

Time:02-17

Say I have a class:

class LiveEntity
{
public:
    int example = 5;
};

Now, in my main file I have two objects of that class:

LiveEntity Box[7];
Box->example = 0;

LiveEntity Circle[7];
Circle->example = 2;

Is there a way for me to alter both Circle->example and Box->example without having to write them separately? Say I wanted both of them to = a certain increment every frame, without having to write them individually one by one inside a for loop. In my actual main.cpp, I have about 10 different objects of the "LiveEntity" class, and I want all of them to be affected by the same addition.

Basically, currently, it looks like this:

for (int x = 0; x < 7; x  ){
    Box[x].example  = increment;
    Circle[x].example  = increment;
    // and 8 more.
}

CodePudding user response:

for (int i = 0; i < 7; i  )
{
    for (LiveEntity *arr : {Box, Circle})
        arr[i].example  = increment;
}

CodePudding user response:

There could be a workaround (possibility) with std::valarray.

Example:

#include <iostream>
#include <valarray>

class LiveEntity {
public:
    int example = 5;
    //we need a default and for convenience an implicit value constructor
    LiveEntity(int val = 5) noexcept : example(val) {}
    //to avoid the implementation of each operator
    //we implement user-defined conversion methods
    operator int  () const noexcept { return example; }
    operator int& ()       noexcept { return example; }
};

//if no user-defined conversion was implemented,
//each operator has to be implemented 
//@see https://en.cppreference.com/w/cpp/numeric/valarray/operator_arith2#Notes
LiveEntity& operator  = (LiveEntity& lhs, LiveEntity rhs) noexcept
{
    lhs.example  = rhs.example;
    return lhs;
}

int main()
{
    //valarray instead of array
    std::valarray<LiveEntity> arr(5);

    //print the default instantiated elements
    for (auto e : arr) {
        std::cout << e.example << std::endl;
    }

    //increase by 1
    //without implicit value constructor,
    //something like arr  = LiveEntity(1) has to be used
    arr  = 1;

    //print the incremented values
    for (auto e : arr) {
        std::cout << e.example << std::endl;
    }

    return 0;
}

CodePudding user response:

Maybe you can define a static member variable to store all the instance of the class, and define a function to update them one by one.

#include <iostream>
#include <set>

using namespace std;

class LiveEntity
{
public:
    LiveEntity() { instances_.insert(this); }
    ~LiveEntity() { instances_.erase(this); }

public:
    int example = 5;

public:
    static void IncrAll(int v)
    {
        for (auto instance : instances_)
            instance->example  = v;
    }

public:
    static set<LiveEntity*> instances_;
    
};

set<LiveEntity*> LiveEntity::instances_;

void Test()
{
    LiveEntity le1;
    LiveEntity le2;
    LiveEntity le3;

    le1.example  = 1;
    le2.example  = 2;
    le3.example  = 3;

    cout << le1.example << " " << le2.example << " " << le3.example << endl;    // 6 7 8

    LiveEntity::IncrAll(3);

    cout << le1.example << " " << le2.example << " " << le3.example << endl;    // 9 10 11
}


int main()
{
    cout << LiveEntity::instances_.size() << endl;

    Test();

    cout << LiveEntity::instances_.size() << endl;

    return 0;
}
  • Related