By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Repost: UnManaged delegates: Do they exist?

P: n/a
Is it possible to create events without using 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.

Mar 19 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> 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.


Hi,

This is possible, but you have to supply both the function pointer and the
object pointer.
This is because in C++ a member function is not explicitly tied to an
instance. Thus the compiler need another way to know which instance the
function should be executed for.

There is a good howto over here:
http://www.newty.de/fpt/fpt.html#chapter2
Take a look at chapter 2.5 which explains how to execute a function with the
member function pointer and the instance pointer

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 19 '06 #2

P: n/a
Egbert Nierop (MVP for IIS) wrote:
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>::PMF m_f;

public:
delegate(T* t, typename function_t<T,RT,E>::PMF 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
Mar 19 '06 #3

P: n/a
Egbert Nierop (MVP for IIS) wrote:
Is it possible to create events without using sendmessage?
SendMessage is totally unrelated to events/delegates.
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.


You'd better use already written libraries for this kind of things. See
boost::signal or libsigc++ for example

Arnaud
MVP - VC
Mar 19 '06 #4

P: n/a

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Egbert Nierop (MVP for IIS) wrote:
Is it possible to create events without using sendmessage?


Events and delegates are completely unrelated ot sendmessage.


Sure, but they are both a way to signal...
sample:
Thanks for your code!

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>::PMF m_f;

public:
delegate(T* t, typename function_t<T,RT,E>::PMF 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


Mar 20 '06 #5

P: n/a
I don't think you need events for what you're trying to do. Function
delegates will do just fine. In the native C++ library space your
closest bet is probably the boost::function library.

Here is an example of how you can achieve this:
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>

class A {
public:
void foo(boost::function<int (bool)> func) {
func_ = func;
}

int foobar(bool b) {
return func_(b);
}

private:
boost::function<int (bool)> func_;
};

class B {
public:
int bar(bool);
};

int main() {
A a;
B b;
a.foo(&b ->* &B::bar);
int result = a.foobar(true); // *** effectively does: result =
b.bar(true);
}

Mar 20 '06 #6

P: n/a
Egbert Nierop (MVP for IIS) wrote:
Is it possible to create events without using 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.


I have two different implementations, both of which are explained in
great detail on my Web site:

http://tweakbits.com/articles/events/index.html

This should give you a good understanding of events.

I recommend that you take a look at boost::function for an
industry-strength solution. Unlike my solution, it's used by 1000s of
programmers on all kinds of platforms.

Tom
Mar 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.