In C + +, it is common to define an object with a class that can have his own attributes (data) and methods (functions), whereas in the normal syntax of C, it is forbidden to define functions in a struct.
In a touch screen driver found that in C language can also implement the object's methods (functions).
First, define a key structure.
1, we first discuss the second function pointer Void (*btn_command) (); We need to assign the pointer to a function pointer of the same type (the function pointer of the invisible parameter) before using the struct-defined object to use this function pointer to implement the method's effect. For example
The name of the function here is the pointer to the function. When you use this method, you only need to
We also think that when we need the method to call the object's own property (data), we change how to do it?
Next we look at the first function pointer.
2, Void (*DRAW_BTN) (void * btn); He also defines a function pointer, but his entry parameter requires an arbitrary type of pointer, and when we use it, we need to convert it to the type of pointer we need (the pointer type of the argument), using the cast type operation. Before using this method, it is also necessary to assign him to a function pointer of the same type function (where the formal parameter is a pointer of any type). For example
In the following function, the parameter is converted to the type of pointer we need (Touch_button *), and in a pointer of the same type, the type's properties can be taken with the same type of pointer.
When you use it, simply pass in the address of the object we need. As follows
How C language Implements object properties and methods in C + +