Function pointers and object-oriented ideas

Source: Internet
Author: User

 Reprinted please indicate the source

Author: Pony


When I first learned C, I didn't pay much attention to function pointers. Later I started embedded software programming. With the accumulation of knowledge, I learned deeply, only then can we find that the role of function pointers in the field of embedded software is so great (other fields are not very clear ). to emphasize the function pointer, we have to mention the object-oriented idea. The concept of Object-oriented has been dominant in software development since its birth. Its powerful functions, in-depth analysis of the software itself, Object-based ideas, and so on, all of which have revolutionalized the methods for software developers to solve problems. because of this, the object-oriented thinking can become popular in all fields of software development, and even has a place in the mainstream embedded software field of C and assembly. For example, analyticdb provides C ++-supported compilers. in Microsoft's open source wince code, we can also see many drivers written in C ++.Program.

However, C ++ also has its drawbacks. its functions and memory collection mechanisms make it cost less efficient than C. this tiny difference may be negligible in other software development fields, but in the field of embedded software, manyCodeIt runs on chips with limited memory space and clock speed, so there are strict requirements on the code size and running speed.

Although the object-oriented language is not available, the idea of object-oriented can be used for reference. The most important concept in object-oriented thinking is class. in some embedded software applications, if this idea is applied, you suddenly find that your code has become "advanced. for example, the author uses the structure and function pointer of C to implement the "class ". similar to the following format

Typedef struct menu

{

Uint8 xposition; // X coordinate of the menu

Uint8 yposition; // y coordinate of the menu

Void (* Action) (void); // The Action triggered when the menu is pressed.

} Menu

Every menu on the LCD is treated as an object. In this way, you only need to assign values to the corresponding struct. This completely subverts the old-fashioned way of judging with if or switch. The idea of class is too powerful here.

The following describes the function pointer. Assume that I write an event handler in the following form:

Void buffer (void) // The parameter type and return value type are determined according to the pointer function declaration.

{

.....

}

I want to call the above function when pressing a menu. I can assign this action to the "member function" of menu in the following form"

Menu;

Menu. Action = (void (*) () buffer;

With the above concept, we can analyze a complex function pointer. In embedded programming, sometimes we need to let the program jump to a specific address for execution. If it is implemented in C, we can use the following form.

(* (Void (*) () 0 )()

It looks complicated. if you split it for analysis, it will be clear at a glance.

First, 0 is the address of the program to be executed. then void (*) () is a function pointer type. it is like int * is a pointer type pointing to the int type. however, this function pointer type also declares the return type and parameter type of the function. the last step is to call this function. when using a function pointer to call a function, this form is similar to (* p. here P is equivalent

(Void (*) () 0.

 

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.