473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Functors

Hello All,
Just want to put up a query with regards to functors.

I read that Functor means a function with state. But what I am not able to understand is the following

, What is the sate
where is the state stored ,
and who is responsible to maintain that state the application or the compiler inherently manages that state.

Is the state accessible to the application , if it is maintained by the compiler.

If possible can somebody give me an example of funtor highlighting the state thing

Regds
Sushobh
Sep 13 '06 #1
3 1870
Banfa
9,065 Expert Mod 8TB
A Functor is a mathematical term. It has no meaning in computing. This doesn't mean you can't model one with a program it just means all your questions about how a compiler handles a functor are irrelevent. It doesn't because it doesn't know what they are.

If you want to modle a functor then your application will have to handle maintaining the state.
Sep 13 '06 #2
Thanks for that,
But what I also want to know , is can I have a example of the state of the functor, I am not very clear on that. Is it possible to provide an example. Maybe it is very trivial thing for you, but I would be grateful if I can have example , explaining that.

Regds
sushobh
Sep 14 '06 #3
Banfa
9,065 Expert Mod 8TB
simple eaxmple of a functor

Expand|Select|Wrap|Line Numbers
  1.  
  2. int functor(int input)
  3. {
  4.     static int functor_state = 50;
  5.  
  6.     if (input < 0)
  7.     {
  8.         functor_state = 100 - functor_state;
  9.     }
  10.     else
  11.     {
  12.         functor_state = (functor_state + input) % 101;
  13.     }
  14.  
  15.     return input % functor_state;
  16. }
  17.  
A function with state, because I have declared functor_state static it is not stored on the stack (if present) but in program memory (a data segment) and it's value is persistent even while the flow of control of the program is outside the function.

Because I have declared functor_state inside the function it is not accessable by any other part of the code.
Sep 14 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: red floyd | last post by:
Disclaimer: VS.NET 2003 (haven't checked any other compiler). I'm writing functors for my classes. Because the objects in my containers are large, I'm making my functors take const T& parameters....
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
2
by: nsgi_2004 | last post by:
Hi, I have been learning about functors and at first everything was clear. Make a class and overload operator () so that an object of the functor can be thought of as a function. However, I...
1
by: Matthias | last post by:
Hello, I basically want to implement a general resource manager which should have a caching and a loading policy so that I can exchange those. Here's some example code of how it should work: ...
4
by: Fraser Ross | last post by:
Functors taking 1 argument for operator() should inherit from unary_function and those with 2 arguments should inherit from binary_function. If a functor has zero arguments for its operator()...
8
by: michael.lesniak | last post by:
Hello, I'm learning C++ for a couple of days and play a bit with the algorithms provided in the STL. One thing I don't understand is the fact that classes inherited of functors have to be...
4
by: tryptik | last post by:
Hello all, I have a question about iterators. I have a container of functors that operate on an std::string. The functors go something like this: class Functor { std::string...
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
9
by: laikon | last post by:
dear, all: below is a function with a parameter of function pointer. void f(int a, int b, int (*fp)(int, int)) { std::cout << fp(a, b) << std::endl; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.