Command Pattern (with func pointers) (C++)

Started by
12 comments, last by NotAYakk 17 years, 12 months ago
Quote:Original post by AAAP
can T be in the form of a whole function prototype such as the return type and parameters? sorry for the newb questions, but this whole topic seems a little over my head at the moment.


With VC6, no, with better compiler, yes:

boost::function<int (double, std::string)> func;

or (VC6)

boost::function2<int, double, std::string> func;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote:Original post by AAAP
I thought boost::function was a implementation of functor?


To be honest, I've never used boost::function so I can't say for sure. However, I thought it was a way to wrap function pointers, pointers to member functions, or functors in such a manner that you could use them interchangeably.

Either way, if you want to use different inputs to different functions, using functors seems the sanest and simplest way to me. Just initialize or update each command object any way you want. Then, when you iterate through and call them just call Execute() or whatever.

FWIW, I've used this approach before in situations like this and it's worked great for me!

-John
- John
Well, I'm just settling for making everything use the same parameters (for commands at least), I guess it's not a big deal.
|aaap.penopticon.com| My website, including game projects. Collaboration/comments are welcome, please visit :)
Quote:Original post by AAAP
can T be in the form of a whole function prototype such as the return type and parameters? sorry for the newb questions, but this whole topic seems a little over my head at the moment.


In my case, no.

When you are dealing with "callbacks", it is often the case that you have the callback pull it's own data from where it needs it, instead of being provided with the data.

If you think about it, if you had a list of functions with two different signatures, how would you know how to call the functions?

Instead, you provide the callback with "too much information", and the callback filters out what it cares about.

This topic is closed to new replies.

Advertisement