mercredi 4 novembre 2020

Variable auto-track: call a function when the variable is set to the same old value and call another function when the variable is set to a new value [duplicate]

I wonder what is the best way in C++ to auto-track a variable so we call a function when the variable is set to same old value and we call another function when the variable is set to a new value. For example,

#include <iostream>

class A{
public: 
    A();
    virtual ~A();
    bool isActive = false;
    
    void IsActiveChanged();
    void IsActiveIsSetToOldValue();

    
};

A::A(){}
A::~A(){}

A::IsActiveChanged(){
    std::cout << " isActive has changed: isActive now =  " << isActive << "\n";
}

A::IsActiveIsSetToOldValue(){
    std::cout << " isActive has set to the same value: isActive now and before =  " << isActive << "\n";
}


int main(){
    A *obj = new A();
    obj->isActive = true;   // this would call A::IsActiveChanged()
    obj->isActive = true;   // this would call A::IsActiveIsSetToOldValue()
    obj->isActive = false; // this would call A::IsActiveChanged()

    delete obj;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire