Coding section:
#include <stdio.h>structstudent{intID; Char*Name; intAge ; void(*tostring) (intIdChar* Name,intAge );};//defines the structure body that contains student ID, name, age, and output function pointers studentvoidToString (intIdChar* Name,intAge ) {printf ("ID value of the mystudent instance variable:%d\n", id); printf ("the name value of the mystudent instance variable:%s\n", name); printf ("Age value of mystudent instance variable:%d\n", age); }//output function, you need to link an internal function pointer to the function when instantiating the struct.intMain () {structStudent mystudent = {1,"Luo Shuai", -}; Mystudent.tostring= toString;//link an external tostring function to a function pointer inside a structMystudent.tostring (Mystudent.id,mystudent.name,mystudent.age);//calling struct intrinsic functions return 0;//Exit Program}
Attached (and the effect of the previous essay is the same, just encapsulated into the structure of the inner function):
The author reminds: Because in C, the structure inside does not allow the definition of function, so can only be defined by the external function, and in the structure of the function pointer, and then through the pointer to link to the function, so as to achieve the effect of invoking the structure of the inner function.
In the next article, we will start to try to use what we have learned, to do a basic calculator function, come on!
C + + Learning Journey-Combat 3-using function functions in struct structs