Link: http://www.cnblogs.com/AnnieKim/archive/2011/11/20/2255813.html
This article is written to deepen your memory. I found that it is hard to remember a lot of knowledge without repeated efforts and hands-on practices.
1) initialize the function pointer.
The function is as follows:
Int comparestring (const string & str1, const string & str2)
{
Return str1.compare (str2 );
}
There are two methods to initialize a function:
The first and most common method:
INT (* comparefunction) (const string &, const string &) = comparestring;
The second method is to use typedef to define the function type, which helps you understand the code:
Typedef int (* comparefunctiontype) (const string &, const string &);
Comparefunctiontype comparefunction = comparestring;
2) function pointer assignment.
The function name can be understood as a pointer to a function of this type. Of course, the address operator can also generate a pointer to this type of function on the function name. That is to say, the following two values are feasible:
Comparefunctiontype comparefunction = comparestring;
Comparefunctiontype comparefunction = & comparestring;
3) function call.
Whether it is called by function name, function pointer, or explicit pointer symbol, the method is the same:
Comparestring ("ABC", "CBA ");
Comparefunction ("ABC", "CBA ");
(* Comparefunction) ("ABC", "CBA ");
4) array of function pointers.
For the array of function pointers, we strongly recommend that you use typedef to define the type before using it. Otherwise, it will affect the readability of the Code. The preceding example is used as an example:
// Without typedef
INT (* comparefunctionarray [3]) (const string &, const string &);
// With typedef
Comparefunctiontype comparefunctiontypearray [3];
5) function pointers are used as types of function return values.
At this step, we will find out how easy typedef is to use. Otherwise, I cannot fully understand the meaning of the following statement:
// Without typedef
INT (* func (int *, INT) (const string &, const string &);
The preceding Declaration declares func (int *, INT) as a function. The returned value is a function pointer and the function type is int (*) (const string &, const string &).
How obscure!
If you write typedef, you don't have to worry about it. The role of typedef is as follows:
Comparefunctiontype func (int *, INT );
6) pointer to the extern "C" function.
C ++ Primer 3 states that pointers to C functions are different from pointers to C ++ functions, but many compilers now have language extensions, the pointers of these two functions have the same characteristics.
So I tried it in vs 2010 and the results proved that it supports this language extension.
The function declaration is as follows:
Extern "C" int insidefunctionc (const string & str1, const string & str2)
{
Return str1.compare (str2 );
}
Int insidefunctioncplusplus (const string & str1, const string & str2)
{
Return str1.compare (str2 );
}
Function pointer initialization and calling. values can be assigned to the pointer pointing to the C function:
INT (* comparefunction) (const string &, const string &) = insidefunctionc;
In addition, when extern "C" is applied to a declaration, all functions declared by it will be affected. For example:
Extern "C" Void outsidefunction (INT (* fc) (const string &, const string &))
{
Cout <FC ("ABC", "CBA") <Endl ;;
}
Both outsidefunction and FC will be affected by extern "C", but the vs2010 compiler supports a pointer to the C ++ function as the parameter of outsidefunction. As follows:
Int main ()
{
Outsidefunction (insidefunctionc );
Outsidefunction (insidefunctioncplusplus );
Return 0;
}
This is almost the case. I read it once yesterday and wrote a blog today to review it again. It should be a better memory. Giggle.
Annoying function pointer (1)