Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1084253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:32:19+00:00 2026-05-16T22:32:19+00:00

What is the problem with this code ? this code is giving me lots

  • 0

What is the problem with this code ? this code is giving me lots of syntax errors. Also I would like to know why functors are used in C++.

class f
{
public:
    int operator(int a) {return a;}
} obj;

int main()
{
    cout << obj(0) << endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:32:20+00:00Added an answer on May 16, 2026 at 10:32 pm

    You’re missing an extra pair of parenthesis when declaring operator(). The name of the function is operator(), and it still needs the list of parameters after it. Thus it should look like:

    int operator()(int a) {return a;}
    

    Function objects (a.k.a. functors) like this are typically used where you’d use a pointer to a function. However, they have the advantage that they can use inheritance and they encapsulate state as well. Often, well designed class or function templates will be able to use them almost interchangeably with function pointers. However, a good optimizer can typically produce better code when a template object is used.

    For a fairly sophisticated example of how you might use function objects, have a look at expression templates.

    Here’s a small, somewhat contrived example of how they can use inheritance:

    struct unary_int_func {
        virtual int operator()(int i) = 0;
    };
    struct negate : public unary_int_func {
        int operator()(int i) {return -i;}
    };
    struct one_plus : public unary_int_func {
        int operator()(int i) {return i+1;}
    };
    
    void show_it(unary_int_func &op, int v) {
        cout << op(v) << endl;
    }
    

    In this case, we create a base class with the operator as a pure virtual function. Then we derive to concrete classes that implement it. Code such as show_it() can then use any instance of a class derived from this base. While we could just have used a pointer to a function that takes an int and returns an int, this is more typesafe. Code that uses the function pointer would accept any such function pointer, whereas this way we can define a whole new hierarchy that maps an int to an int:

    struct a_different_base_class {
        virtual int operator()(int i) = 0;
    };
    

    but instances of this would not be interchangeable with instances of unary_int_func.

    As for state, consider a running sum function:

    struct running_sum : public unary_int_func {
        int total;
        running_sum() : total(0) {}
        int operator()(int i) {return total += i;}
    };
    
    int main()
    {
        running_sum s;
        cout << s(1) << endl;
        cout << s(2) << endl;
        cout << s(3) << endl;
        cout << s(4) << endl;
    }
    

    Here, the instance of running_sum keeps track of the total. It will print out 1, 3, 6 and 10. Pointers to functions have no such way of keeping state between distinct invocations. SGI’s STL page on function objects has a similar example to my running sum one, but shows how you can easily apply it to a range of elements in a container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
This is an erlang problem, it seems. I have this code to test the
I am having some trouble with this code . The problem is when i
I am trying to convert this test code to C# and having a problem
I've got a problem with inheritance and generics. This is the code that illustrates
Expanding this question on how I learnt to pass from problem description to code
I want to create maintainable code, but this inheritance situation is causing me problems.
I have been puzzling over a problem this morning with LinqToSQL. I'll try and
Consider this problem: I have a program which should fetch (let's say) 100 records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.