jeudi 14 octobre 2021

Knowing when pointed to object changes

In C++ is there a way, using raw pointers or otherwise to trigger some action when the pointed to object changes?

Scenario:

class A
{
    double var;
    B var2 {&var};
}

class B
{
   double* pVar;
   B (double* _var ) { pVar = _var};
}

I have functions inside class B that will get called whenever member variable var changes value. At the moment I would need these functions to be public so they can be called manually from class A, this could be achieved by using a setter for var. If I wanted to keep the function inside class B private (as it is called by other events internal to class B) what are my options?

Aucun commentaire:

Enregistrer un commentaire