How C language Implements object properties and methods in C + +

Source: Internet
Author: User

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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.