WTF are functors, and how do they work?

Started by
10 comments, last by Toolmaker 20 years, 6 months ago
Well, as the title says, what are they, and how do they work? A few good examples are welcome, aswell as a good tutorial on them. I didn''t understand the stuff on www.sgi.com about them. In case you wanna know,someone told me it would be usefull to implement them in my win32 wrapper, instead of static functions. Toolmaker -Earth is 98% full. Please delete anybody you can.

Advertisement
I assume that you spelled wrong and are talking about functions?
If you are look below other wise forget this post.
--------------------------------
Functions are just as they sound, they carry out commands, etc.
For example:
int new_file(char *filename, char ipt[80]){   ofstream datafile001( filename );   datafile001 << ipt1 << endl;   datafile001.close();   return(1);}////////////In your Game_Init functionnew_file("test.txt", "This is a test");
Tutorials would be good at http://www.functionx.com/
you can find what your looking for easily.
Here''s a good tutorial on functors.
summary: functor are objects with an operator() so they can be called like functions. The advantage is that they can have infromation associated with that object.

most common use to to write a mem_fun() functor.
quote:Original post by demonrealms
I assume that you spelled wrong and are talking about functions?
If you are look below other wise forget this post.
--------------------------------
Functions are just as they sound, they carry out commands, etc.
For example:
int new_file(char *filename, char ipt[80]){   ofstream datafile001( filename );   datafile001 << ipt1 << endl;   datafile001.close();   return(1);}////////////In your Game_Init functionnew_file("test.txt", "This is a test");
Tutorials would be good at http://www.functionx.com/
you can find what your looking for easily.

No he didn''t spell it incorrectly, "functors" do exist you know.
In the C++ sense they are typesafe callbacks that work on member functions or regular function. Use Boost::Function with Boost::Bind to make them.

They mean something entirely different in ML.

Oh, and you still need static functions for Win32 callbacks.

[edited by - antareus on October 20, 2003 9:55:51 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Well, I am not looking for callback functions . I am using oluseyi''s way of having a windows class and such. I am currently writing a pretty large wgBaseWindow class and I wanted to use functors for that. So I can do this:

class ExitButton{public:     long Click(wgBaseWindow *Owner, wgBaseWindow *Me, WPARAM wParam, LPARAM lParam);}; 


And then pass the Click function into the message line. Is it possible that way?

Toolmaker


-Earth is 98% full. Please delete anybody you can.

Don''t know what you mean by message line, but to
connect events, you might find libsigc++ useful.
If have a special RegisterEvent function to get fn pointers. However, the functions need to be static. How do I make then non-static, so I can just use class members without having the function static.

Toolmaker

I just explained what you had to do but you shot back "I don''t need callbacks."

Make up your mind.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement