A summary of function pointers and function objects in C + + _c language

Source: Internet
Author: User

Chapter One, function pointers
function pointer:
is a pointer variable to a function, in C compilation, each function has an entry address, then the function pointer to this point to this address.

function pointers are used for a large purpose, with two main functions: as arguments for calling functions and functions.

Method of declaring function pointers:
Data type identifier (pointer variable name) (parameter list);
The Declaration of a general function is:
int func (int x);
The method of declaring a function pointer is:
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 pointer, which is dependent on the function parameter that the function pointer points to.
However, we sometimes find it very cumbersome to declare this, so the typedef can come in handy, and we can say:
typedef int (*PF) (int x);
PF PF;
So PF is a function pointer, convenient for many. When you want to use the function pointer to call the function, func (x) or (*FUCN) (x) is OK, of course, the function pointer can also point to the overloaded function, the compiler will distinguish these overloaded functions for us so that the function pointer points to the correct function.
Example:

Copy Code code as follows:

typedef void (*PFT) (char, int);
void Bar (char ch, int i)
{
cout<< "Bar" <<ch<< ' <<i<<endl;
return;
}
PFT PFT;
PFT = bar;
PFT (' E ', 91);

In the example, the function pointer pft points to a function bar () that has already been declared, and then uses the PFT to achieve the purpose of the output character and integral type.
The function pointer is another function of the function as an argument, we can pass a function pointer in the parameter list of a function, and then we can use the function pointed to by the function pointer in this function, which can make the program more clear and concise, and this use technique can help us solve many difficult problems. , you can get a big enough benefit (speed + complexity) with a small price.
Copy Code code as follows:

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;
}
PFT PFT;
PFT = bar;
Foo (' E ', 12,pft);

For the above example we first use a function pointer pft to point to Bar () and then use the PFT pointer in the Foo () function to invoke bar () to achieve the purpose. Using this feature slightly, we can construct powerful programs that require the same Foo function to invoke different bar functions.

Chapter Two, Function object
The front is the application of the function pointer, in the sense of general function callback, function object and function pointer are the same, but function object has a bit of function pointers, function object makes program design more flexible, and can realize the function's inline (inline) call, so that the whole program realizes performance acceleration.

function object: Here it is explained that this is an object, and it is actually just some function of the function of the object, we call it the function object, the meaning is very appropriate, if an object has the function of a function, we can call it the function object.
How do you make an object function, simply by overloading the operator () of the object, as follows:

Copy Code code as follows:

Class a{
Public
int operator () (int x) {return x;}
};
A A;
A (5);

So that 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 symbol of C, class is specific to C + +, then we can also say that the relationship between the pointer function and function object is the same as the previous! (albeit somewhat closely). When we want to call a function in the parameter list, we can declare a function object with this function function, and then use the object in the parameter, the function and function pointers made by him are the same and more secure.
Here is an example:
Copy Code code as follows:

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;
}
Func Func;
Addfunc (1,3,func);

The above example first defines a function object class, and overloads the () operator to add and output the first two parameters, and then use the class object in the parameter list in Addfunc to achieve the function of adding two numbers.
If you consider using generic thinking, you can set a function template class to implement the addition of general types of data:
Copy Code code as follows:

Class funct{
Public
Template<typename t>
T operator () (t T1, T T2)
{
cout<<t1<< ' + ' <<t2<< ' = ' <<t1+t2<<endl;
return t1;
}
};
Template <typename t>
T addfunct (t T1, T T2, funct& funct)
{
Funct (T1,T2);
return t1;
}
Funct funct;
Addfunct (2,4,FUNCT);
Addfunct (1.4,2.3,FUNCT);

This technique is widely used in the famous STL. See the details of the master Hou jie some of the generic technology books, do not think that the frequent invocation of function objects will greatly reduce the performance of the program, a large number of facts and experiments prove that the correct use of the function object program is much faster than other programs performance! So master and skilled use of function objects to our program bonus points, otherwise ....
In this way, the function object has opened a skylight for C + +, but with it comes some complex problems and traps, how to cover Yanli also need us to continue to learn and explore.

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.