Archived

This topic is now archived and is closed to further replies.

Toolmaker

WTF are functors, and how do they work?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 function

new_file("test.txt", "This is a test");
Tutorials would be good at http://www.functionx.com/
you can find what your looking for easily.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 function

new_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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Ok, I misunderstood you. But, why use boost? As far as I can see, I just need to be able to store a function pointer to something like myclass::foo(...).

I might be stupid or stubborn(spelling?), but I rather don''t use a large library for this. If someone can explain this to be, thx, otherwise, I gotta use boost...

Toolmaker



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

Share this post


Link to post
Share on other sites
Please do not fall into Gamedev.net "Not Written Here Syndrome." Boost is not a large library:
* it is header-only with a few exceptions
* the linker strips out every definition not used in your final program anyway

You use boost for reasons I already mentioned in another thread:
They use a preprocessor written in C++ to generate templates for function pointers taking 1-50 arguments. These arguments can be of any type, but you have to have one definition of the function (called boost::functionN, where N is a number) for each number of arguments in the callback.

They are type-safe and easy to work with once you get the hang of them. In short, don''t look at gift horse in the mouth, I''ll take stuff they write over anyone''s homebrewed implementation (often the only justification people give for it is "it isn''t my naming convention.") Several of them are on the C++ standards committee, and they are quite smart.

Share this post


Link to post
Share on other sites