|
Greetings,
I just recently started some experiments with C++. I'm an experienced programmer, but not with C++, but, just for fun, I decided to implement this example C++ Delegate here on Code Project.
This example keeps the methods hooked into the delegate inside structs. In the example given these are: struct TShapes, struct TDerivedShapes and struct TThings. These are then initialized in this fashion: event += new MyEvent::T<tshapes>(&shapes, &TShapes::Square);
My question is this: is it necessary to store the methods to be hooked into these events into a struct? I assume this is done because the struct makes the address available to be used in the delegate. If the answer to this question is no, then how do you hook into the even just through standard methods within a normal C++ program? Please forgive the stupidity of my questions.
Ed.
|
|
|
|
|
Take a look at function pointers[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Well, thanks, but that's not quite what I asked.
I was wondering if it was required to embed your methods in a struct for the example identified. If by your response you meant "yes, they must be embedded" then an explicit answer would be appreciated.
Thanks again.
Ed.
|
|
|
|
|
Edward G Dana wrote: Well, thanks, but that's not quite what I asked. Sorry, I misunderstood. But I think your question should really be directed at the author of the article.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
C++ doesn't natively support delegates and delegate implementation in C++ is quite messy especially if you try to support methods with variable number of parameters and parameter types - it also involves super-ugly method pointer casting. Delegates are simply ugly in C++, I recommend never using them if it isn't necessary. (For me using delegates is necessary only if a used 3rd party library makes it necessary to use delgates in its public interface...) I always prefer using old-school listener interfaces with old-school virtual methods (classes with pure or empty-bodied virtual methods only) to listen for events. Always consider alternatives and prefer readability of the resulting code. It doesn't worth trying to shape C++ into C#!
|
|
|
|
|
This is just me experimenting and playing. Seeing what I can do and make C++ do, for fun.
|
|
|
|