In Java, to implement a certain time to invoke a piece of code is very simple thing, that is, the use of interfaces.
In C + +, there is a more advanced way, that is, the use of function pointers.
For example, Cocos2d-x timers (schedule), message subscriptions (notificationcenter) Use function pointers to perform callback functions.
That's why we can always pass a function as a parameter, and then at some point the function is invoked.
One, the address of the function
To get the address of an int variable is very simple, such as int num; Then num's address is &num.
And getting the address of the function is simpler, the function's name is the address of the function, the following code:
Copy Code code as follows:
void Hello ();
int _tmain (int argc, _tchar* argv[])
{
Auto p = Hello;
P ();
return 0;
}
void Hello ()
{
cout << "HelloWorld";
}
We define a hello function and assign the function name directly to the pointer p, so we can use p as the Hello function.
It's very simple.
Second, declare function pointers
Getting the address of a function is simple, but it becomes less straightforward to declare a function pointer.
We can't always use auto to run away from each other, can we? Sometimes we have to explicitly declare function pointers, so how do we declare them?
Do you remember what we said about the TypeDef definition type alias? The declaration of a function pointer is also the same rule, first declaring a function, such as: void Hello ();
Then change the function name to a pointer, such as: Void (*p) ();
Yes, it's that simple, void (*p) (); is void hello (); 's statement.
Try again immediately, this function: int getValue (float dt);
What is its function pointer declaration? Yes, that is: Int (*p) getValue (float DT);
Yes, it's that simple. int getValue (float dt); is the int (*p) getValue (float dt), the function pointer declaration.
Try again at once, this. (Jor: Stop ~! Don't think you can mess with me if I'm not here! )
Well, then don't go on, let's see, just the code can write this:
Copy Code code as follows:
void Hello ();
int _tmain (int argc, _tchar* argv[])
{
void (*p) ();
p = Hello;
P ();
(*p) (); Added the phrase secretly.
return 0;
}
void Hello ()
{
cout << "HelloWorld";
}
OK, very simple, not much to say ~
In addition, have you found that I secretly added a code?
Yes, by (*p) (), and by the way it succeeds in calling the Hello function, why?
Third, historical reasons
Since P is the pointer, it points to the address of the Hello function, so *p represents the Hello function, so (*p) () equals Hello (), which is normal logic.
So, actually (*p) () is the more normal way of calling.
However, because the function name is the pointer to the functions, that is, hello actually points to the address of the function.
In other words, p and hello are pointers, so the way P is invoked is the same as the way hello is called, so P () is the equivalent of Hello ().
Both of these ways are correct, in fact, the grammar of this thing, is determined by the predecessors of the history of these two ways to see each other, so that they tolerate the two seemingly conflicting ways at the same time.
However, I think most people would prefer to use P () instead of (*p).
Four, the TypeDef saves the complex function pointer
The following code:
Copy Code code as follows:
string hehe1 (int num, float value);
string hehe2 (int num, float value);
string hehe3 (int num, float value);
int _tmain (int argc, _tchar* argv[])
{
/* Declare function pointer array/*
String (*p[3]) (int num, float value) = {hehe1, hehe2, hehe3};
string result = P[1] (1, 2);
cout << result.c_str () << Endl;
return 0;
}
string hehe1 (int num, float value)
{
return "HAHA1";
}
string hehe2 (int num, float value)
{
return "HAHA2";
}
string hehe3 (int num, float value)
{
return "HAHA3";
}
This code has three parameters and return values are the same function, respectively, HEHE1, Hehe2, Hehe3
Then, we're going to declare an array that holds the three function pointers.
The function here is relatively simple, so it doesn't look complicated.
But if there are too many such statements, it would be frustrating.
So the typedef saved us, and we can make a complicated statement like this:
Copy Code code as follows:
int _tmain (int argc, _tchar* argv[])
{
/* Use Hehefunc to replace complex function declarations.
typedef string (*HEHEFUNC) (int num, float value);
/* Declare function pointer array/*
Hehefunc P[3] = {hehe1, hehe2, hehe3};
string result = P[1] (1, 2);
cout << result.c_str () << Endl;
return 0;
}
After using a typedef instead of a function declaration, we can easily use it and make our representatives simple and understandable.
Now, Hehefunc represents a type, what type? is the parameter (int num, float value) that returns the type of the function that has the value string.
V. End
All right, let's talk so much.