Defining function objects

Source: Internet
Author: User

Although function pointers are widely used to implement function callbacks, C + + provides an important method of implementing callback functions, which is the function object. The function object (also known as "operator") is a generic class object that overloads the "()" operator. So syntactically, the function object is similar to the normal function behavior.

There are several advantages to using function objects instead of function pointers, first, because objects can be modified internally without altering external interfaces, so the design is more flexible and more flexible. The function object also has data members that store the results of the previous invocation. You need to store the results of a previous call in a full or local static variable while using a normal function, but there are some flaws that we don't want to see in the full or local static variables.

Second, the compiler can implement inline calls in the function object, further enhancing performance. This is almost impossible to implement in the function pointer.

The following example shows how to define and use a function object. First, declare a normal class and overload the "()" Operator:

class Negate
{
public:
int operator() (int n) { return -n;}
};

Overload Action statement, remember that the first parenthesis is always empty because it represents an overloaded operator name, and the second parenthesis is the argument list. In general, when overloading an operator, the number of parameters is fixed, while overloading the "()" operator is different, it can have any number of parameters.

Because the operations built in negate are unary (only one operand), the overloaded "()" operator also has only one parameter. The return type is the same as the parameter type-in this case int. function returns an integer opposite the parameter symbol.

Working with Function objects

We now define a function called callback () to test the function object. Callback () has two parameters: one is int and one is a reference to the class negate. Callback () Neg the function object as a normal function name:

#include <iostream>
using std::cout;
void Callback(int n, Negate & neg)
{
int val = neg(n); //调用重载的操作符“()”
cout << val;
}

Not in the code, notice that neg is an object, not a function. The compiler will statement

int val = neg (n);

Converted to

int val = neg.operator () (n);

Typically, a function object does not define constructors and destructors. As a result, no problems occur during the creation and destruction process. As mentioned earlier, the compiler can inline overloaded operator code, so it avoids run-time problems associated with function calls.

To complete the above example, we use the main function main () to implement the parameter pass of callback ():

int main()
{
Callback(5, Negate() ); //输出 -5
}

This example passes the integer 5 and a temporary negate object to callback (), and then the program outputs-5.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.