Egbert said:
Is it possible to create events without using sendmessage?
Events and delegates are completely unrelated ot sendmessage.
sample:
class a can retrieve a pointer to a member function of class b that
instanciates class a?
class b, sets the pointer to its member using & (the mechanism I"m
looking for) and passes it to the instance of class a.
A managed delegate is an instance of a class (System.MulticastDelegate).
That class encapsulates a few things, including a method selector
(equivalent to a pointer to member function) and an object reference.
You can build the same thing in native C++, or you can use an already
written library like boost::signals.
The native equivalent to a delegate is roughly:
template <class T, class RT, class E>
struct function_t
{
typedef RT (T::*PMF)(void*,E*);
};
template <class T, class RT, class E>
class delegate
{
private:
T* m_t;
typename function_t<T,RT,E>:
MF m_f;
public:
delegate(T* t, typename function_t<T,RT,E>:
MF f)
: m_t(t), m_f(f)
{
}
RT operator() (void* o,E* e) const
{
return (m_t->*m_f)(o,e);
}
};
class E
{
};
class A
{
public:
int f(void*, E*);
};
int main()
{
A a;
delegate<A,int,E> d = delegate<A,int,E>(&a,&A::f);
// ...
int i = d(0,new E());
}
Of course, unlike a managed delegate, an instance of this native delegate
won't keep the referenced object alive - you still need to ensure that the
object lifetime exceeds the delegate lifetime yourself.
-cd