Once again I'm stuck with some class design issue. I have a class that has a reference of another class or "extends" (I believe it's the correct word) another class. If that other class is changed, how can I get the class that referenced it to know that it has changed and triggered an execution of a method? Maybe it's confusing, but the pesudocode is pretty much like the code below:
What I want to do is changing the integer in "varA" object and also trigger the "varB" which referenced it to call the method "calc()". So it's gonna be like this:Code:class A { public: A(int a): IntA(a); void setInt(int theInt) { mIntA = theInt; } int getInt() { return mIntA; } ~A(); protected: int mIntA; } class B { public: B(A* theA, int mult) { mTheA = theA; mMultiplier = mult; mResult = theA->getInt() * mult; } void changeMult(int mult) { mMultiplier = mult; calc(); }; void calc() { mResult = mMultiplier * theA->getInt(); }; protected: A* mTheA; int mMultiplier; int mResult; }
Code:int main () { A* varA = new A(1); B* varB = new B(A, 2); //A->mIntA=1 ; B->mMultiplier=2 ; B->mResult=2 varB->changeMult(4); // B->mMultiplier=4: A->mIntA=1 ; B->mResult=4 //Change var A and automatically call B->calc() varA->setInt(4) //B->mMultiplier=4; A->mIntA=4 ; B->mResult=4*4=16 }
How do I do that?
Thanks in advance.



LinkBack URL
About LinkBacks



CornedBee