Summary of function pointers and function objects in C + +

Source: Internet
Author: User

First, function pointers

A function pointer , which represents a pointer to a function that refers to a pointer variable pointing to a function, and when C compiles, each function has an entry address, then the function pointer to the function points to the address.

function pointers are used for a large part, with two main functions:

1, as a calling function

2, do the parameters of the function.

How to declare a function pointer :

Data type identifier (pointer variable name) (parameter list);

The Declaration of a general function is:

int func (int x);

A function pointer is declared as follows:

Int (*func) (int x);

The preceding (*FUNC) bracket is necessary, which tells the compiler that we are declaring a function pointer instead of declaring a function with a return type as a pointer, and the subsequent parameter depends on the function parameter that the function pointer points to. However, this statement is sometimes very cumbersome, so typedef can come in handy, we can also declare:

typedef int (*PF) (int x); PF PF;

So PF is a function pointer , which facilitates a lot. When you use a function pointer to invoke a function, Pf=func, pf (x) is available, and of course, the function pointer can also point to the overloaded function, and the compiler will differentiate these overloaded functions for us so that the function pointer points to the correct function.
Example:

#include <iostream>using namespace std;typedef void (*pft) (char, int), void Bar (char ch, int i) {cout<< "bar" <<ch<< ' <<i<<endl;return;} int main () {PFT pft;pft = bar;pft (' e ',;); system ("Pause");}

In the example, the function pointer , PFT, points to a function bar () that has been declared, and then uses the PFT to achieve the output character and integer type.

function Pointers Another function is as a function parameter , we can pass a function pointer in the formal parameter list of a function, then we can use this function pointer to the function in this function, This makes the program clearer and more concise, and the use of this technique can help us solve a lot of tricky problems, and at a very small cost, we can get a large enough benefit (speed + complexity).

Example:

#include <iostream>using namespace std;typedef void (*pft) (char, int), void Bar (char ch, int i) {cout<< "bar" <<ch<< ' <<i<<endl;return;} void foo (char ch, int i, PFT pf) {pf (ch,i); return;} int main () {PFT pft;pft = bar;pft (' e ',;); system ("Pause");}

In the example above we first use a function pointer pft to point to Bar () and then use the PFT pointer in the Foo () function to invoke bar () for the purpose. By taking advantage of this feature, we can construct powerful programs that require the same Foo function to make calls to different bar functions.

Second article, function object

It has been explained here that this is an object, and actually only some function of the function of this object, we call it the function object , it is very appropriate, if an object has the function of a function, we can be called function object .

The front is the application of the function pointer , from the general function callback sense, the function object and the function pointer is the same, but the function object has many function pointers do not have a bit, Function Objects make programming more flexible and enable inline (inline) invocation of functions, enabling the entire program to achieve performance acceleration.

So, how to make an object function functions, very simple, only need to be overloaded with the operator () of this object, as follows:

#include <iostream>using namespace Std;class a{public:int operator () (int x) {Cout<<x<<endl;return x;}}; int main () {A a;a (5); System ("Pause");}

So a becomes a function object , and when we execute a (5), we actually take advantage of the overloaded notation ().

Since the function object is a "class object", then of course we can call it in the function parameter list, it can completely replace the function pointer ! If the pointer is a C flag and the class is unique to C + +, then we can also say that the relationship between the function pointer and the function object is the same as the previous person! (though somewhat tight).

When we want to call a function in the formal parameter list, we can declare a function object with this function function, and then use this object in the parameter, the function and function pointer of his function are the same, and more secure.

Example:

#include <iostream>using namespace Std;class func{public:int operator (int a, int b) {cout<<a<< ' + ' <<b<< ' = ' <<a+b<<endl;return A;}}; int Addfunc (int a, int b, func& func) {func (A, b); return A;} int main () {Func func;addfunc (1,3,func); System ("Pause");}

The above example first defines a function object class and overloads the () operator to add and output the first two arguments, and then use the class object in the parameter list in the Addfunc to achieve the function of adding two numbers.

If you use generic thinking to consider, you can set a function template class , to achieve the general type of data addition:

#include <iostream>using namespace Std;class funct{public:template<typename t>t operator () (t T1, T T2) {cout <<t1<< "" <<t2<<endl;return T1;}; Template <typename t>t addfunct (t A, T B, funct& funct) {funct (A, b); return A;} int main () {funct funct;addfunct (2,4,funct); addfunct (1.4,2.3,funct); Addfunct ("Hello", "World", funct); System ("Pause ");}

This technology is widely used in the well-known STL, which can be found in the books of some of the generic technologies of the Master of the Czech Republic, and do not assume that the frequent invocation of functions on elephants will greatly discount program performance, and a large number of facts and experiments prove that the function objects are used correctly Program is much faster than other programs! So mastering and skillfully using function objects can add points to our program.

In this case, the function object also open a skylight for C + +, but it comes with some complicated problems and traps, how to cover Yanli still need us to continue to learn and explore.

Summary of function pointers and function objects in C + +

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.