Function type by its return class type and the parameter type joint decision:
1 // compare two lengths of string objects 2 BOOL lengthCompare ( const string &, const string &);
The type of the above function is: BOOL (const String &, const String &) , if you want to declare a pointer to this function , only need to replace the function pointer name. as follows:
bool (PF *) (const string &, const string &) ; // PF points to a function, the function parameter is a reference to two const string, the return value is of type bool.
We observed that the above statement: * There is, that it is a pointer to the front pf; right side is the parameter list, described pf points is a function, wherein the function parameter is a reference to a const string, the return value is of type bool.
If not, "brackets", then pf is a return value bool * function, meaning wrong.
First, the use of function pointers:
1. When we put a function name is used as a value, the function is automatically converted to a pointer.
pf = lengthCompare; // pf points to a function called lengthCompare
pf = & lengthCompare; // equivalent formula and wherein "&" symbol is optional address-
2. We can directly use the pointer to a function call to the function, without pointer dereference in advance.
bool b1 = pf ( "hello", "goodbay"); // call the function lengthCompare
bool b2 = (* pf) ( "hello", "goodbay"); // and an equivalent formula as call
bool b3 = lengthCompare ( "hello", "goodbay"); // and an equivalent formula as call
Conversion rule does not exist between the different points to the function type (different type or different return parameter type) pointer;
II (heavy load function) pointer:
When we use overloaded functions, if the definition of the function pointer points to reload, a certain type must exactly match the pointer is overloaded functions.
void ff(int *);
void ff(unsigned int);
void (* pf1) (unsigned int) = ff; // for the above two overloads, pf1 point ff (unisgned)
Third, the function pointer is used as a parameter:
Katachisan:
Although not as a function of the parameter, but may be a pointer to a function of the parameter, as can the array as a parameter, but can be a pointer to an array used as a parameter.
useBigger void (const String & S1, S2 const String &, BOOL PF (const String &, const String &) ); // The third parameter is a function of the type, it will automatically be converted to the type of function pointers to functions frontal
useBigger void (const String & S1, S2 const String &, BOOL (PF *) (const String &, const String &) ); // Explicit parameter will be defined as a pointer to a function
Arguments:
We can be directly used as a reference shape function, then the function will be automatically converted to a pointer:
useBigger (S1, S2, lengthCompaer ); // will automatically be converted to a pointer to the function of the function
Type of use aliases to solve the above-mentioned function declaration is more lengthy question:
typedef bool Func (const string &, const string &); // Func is a function of the type
typedef decltype (lengthCompaer) Func2; // equivalent type
typedef bool (* FuncP) (const string &, const string &); // pointer to function
typydef decltype (lengthCompare) * FuncP2; // equivalent type
void useBigger (const string &, const string &, Func); // Func compiler will automatically convert a pointer to point to a function of type
void useBigger(const string&,const string&,FuncP2);
Fourth, return pointer to a function:
And an array of similar, though not a function returns, but can return a pointer to a function, but we must be written in the function returns a pointer type form, the compiler will not automatically return type of the function as a pointer corresponding to the type of treatment.
using F = int (int *, int); // F is a function of the type, not the pointer
using PF = int (*) (int *, int); // PF pointer type
PF f1 (int); // correct, PF is a pointer to a function, f1 return pointers to functions
F * f1 (int); // correct, return type is specified display pointer to function