C ++ function objects differ from function pointers.

Source: Internet
Author: User

In the C ++ programming language, many functions are connected with the C language, such as pointer applications. Here we will introduce a C ++ function object similar to a function pointer. The C ++ 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.
The C ++ function object is essentially a class that implements the operator () -- parentheses operator.
The following is an example of a function object and a function pointer:

Namespace {class addcls {public: int operator () (int A, int B) {return a + B ;}; int addfunc (int A, int B) {return a + B ;}} void test_addobj () {// define the function object addcls addobj; print_debug (addobj (3, 4);} void test_addfunptr () {typedef int (* funptr) (int A, int B); funptr addfunptr = & addfunc; print_debug (addfunptr (3, 5 ));}

The usage is the same except for the definition method.
Since the usage of C ++ 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 ;}; less isless (10); cout <isless (9) <"" <isless (12); // output 1 0

Another example:

Const int size = 5; int array [size] = {50, 30, 9, 7, 20 }; // locate the int * pA = STD: find_if (array, array + size, less (10) position smaller than the first number of 10 in the array )); // position where pa points to 9 // locate the position where the number is smaller than the first number of 40 in the array. int * pb = STD: find_if (array, array + size, less (40); // point Pb TO 30

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 // No problem with function pointers: bool less10 (INT v) {return v <10 ;}cout <count_n (array, size, less10); // 2

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.