C ++ pointer (4) function objects

Source: Internet
Author: User
The function object is not a function pointer. However, in the program code, it is called in the same way as the function pointer, followed by a bracket.
This is an entry-level article about the definition, usage, and relationship with function pointers and member function pointers of function objects.
Mu Feng Xiaozhu
A function object is essentially a class that implements the operator ()-Parentheses operator.
For example: Class Add
{
Public:
Int operator () (int a, int B)
{
Return a + B;
}
};

Add add; // define a function object
Cout <add (3, 2); // 5

The function pointer version is: Int AddFunc (int a, int B)
{
Return a + B;
}
Typedef int (* Add) (int a, int B );

Add add = & AddFunc;
Cout <add (3, 2); // 5

The usage is the same except for the definition method. All are: Cout <add (3, 2 );

Since the usage of function objects and function pointers is no different, why should we use function objects? It's easy. Function objects can carry additional data, but pointers won't work.
The following is an example of using additional data: Class less
{
Public:
Less (int num): n (num ){}
Bool operator () (int value)
{
Return value <n;
}
Private:
Int n;
};

When using: Less isLess (10 );
Cout <isLess (9) <"" <isLess (12); // output 1 0

In this example, it seems to be too fun: Const int SIZE = 5;
Int array [SIZE] = {50, 30, 9, 7, 20 };
// Locate the first number smaller than the array and smaller than 10
Int * pa = std: find_if (array, array + SIZE, less (10); // the position where pa points to 9
// Locate the first number smaller than the array and smaller than 40
Int * pb = std: find_if (array, array + SIZE, less (40); // point pb to 30.

The convenience of function objects can be seen here? The advantage of function objects is that additional data can be stored in function objects.
Its weakness is also obvious. Although it is used as a function pointer, it is not a real function pointer after all. In the use of function pointers, it is powerless. For example, you cannot pass the function object to the qsort function! Because it only accepts function pointers.

To make a function accept both function pointers and function objects, the most convenient method is to use a template. For example: Template <typename FUNC>
Int count_n (int * array, int size, FUNC func)
{
Int count = 0;
For (int I = 0; I <size; ++ I)
If (func (array [I])
Count ++;
Return count;
}

This function can count the number of qualified data in the array, for example: Const int SIZE = 5;
Int array [SIZE] = {50, 30, 9, 7, 20 };
Cout <count_n (array, SIZE, less (10); // 2

There is no problem with using function pointers: Bool less10 (int v)
{
Return v <10;
}
Cout <count_n (array, SIZE, less10); // 2

In addition, the function object has a function pointer that is unmatched: it can be used to encapsulate class member function pointers!
Because the function object can carry additional data, and the member function pointer lacks a Class Object (class instance) pointer for calling, you can save the class object pointer to the function object, you can call the corresponding class Object member function.

Template <typename O>
Class memfun
{
Public:
Memfun (void (O: * f) (const char *), O * o): pFunc (f), pObj (o ){}
Void operator () (const char * name)
{
(PObj-> * pFunc) (name );
}
Private:
Void (O: * pFunc) (const char *);
O * pObj;
};

Class
{
Public:
Void doIt (const char * name)
{Cout <"Hello" <name <"! ";}
}; A;
Memfun <A> call (& A: doIt, & a); // save a: doIt pointer for calling
Call ("Kitty"); // output Hello Kitty!

Now, the member function pointer can be easily saved for future calls.

However, the reality is cruel. Although a function object can store member function pointers and call information for calling like a function pointer, it has limited capabilities. A function object is defined, only one member function pointer with a specified number of parameters can be implemented.
Mem_fun in the standard library is such a function object, but it can only support 0 and 1 parameter member function pointers. For example, int A: func () or void A: func (int), int A: func (double), and so on. To add another parameter, such as: int :: func (int, double), sorry, not supported. If you want it, you only need to write it yourself.
What's more, how many can we write? Five? 10? Or 100 (which is terrible )?
Fortunately, the boost Library provides the boost: function class, which supports 10 parameters by default, and supports up to 50 function parameters (more). Generally, this is enough. However, its implementation is terrible: with some special and macro definitions of the template, dozens of template parameters are obtained, and dozens of function objects are biased (during compilation.

----
A proposal accepted by C ++ 0x is a list of variable template parameters. With this technology, you do not need to specialize in countless function objects. As long as one function object template can solve the problem. Look forward to it.

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.