Let’s say I’ve got some class that observe, and a class that is observed, I want to notify observers, but in a presented way.
class that is observed has a array of pointers to methods in class that observe.
So basically when I call notifyObservers in observed class, it would go through this array and simply call necessary functions in observers.
That solution was presented by my teacher, and I have hard time making it work, he sad that this solution doesn’t require holding pointer to obervers (like Observer design pattern) only pointers to methods. But how do I store pointers to a method that know who should call them
I tried something like this:
class A {
public:
void method();
}
A a;
void(A::*ptr)()=&a.method;
void (*ptr)()=&a.method;
Which obviously didn’t work.
Is there even a way to make it work?
Try using bind:
Note that as betabandido mentioned, this works only in C++11 as is or in older versions using boost.