Interview Question
Country: United States
Sounds like a typical "observer pattern" scenario. B has to be "observable" and A has to be an "observer" of B's properties.
For me, the question is not quite clear, but this is "an" answer to it (what exactly is "whatever that happens to...")
class A {
void ReceiveMessage(Message m)
{
/* Do Something with it! */
};
};
class B {
A* a_instance; // An instance of A
// Link to an instance of A
B(A* x) {
a_instance = x;
};
/* DO SOMETHING */
void DoSomething()
{
// something
Message m;
// m <- whatever you want to tell A.
UpdateA(m);
};
void UpdateA(Message m) {
a_instance->ReceiveMessage(m);
};
};
A a;
B b(&a);
B.DoSomthing();
Create class-B as base of class-A with public access. So all the things happening in base class(B) can be visible to derive class(A).
- Rahul September 15, 2013