function pointer __c++ in C language

Source: Internet
Author: User

The function pointer is difficult to use, not because it is difficult to understand the syntax, but in the appropriate situation

Use of function pointers:

is related to layered design. layered design has long been a new concept, the benefits of layering are well known, and the obvious benefit is simplifying complexity and isolating changes. Layered design, each layer only care about their own things, which reduces the complexity of the system, the interaction between layers is limited to a very narrow interface, as long as the interface does not change, a layer of changes will not affect the other layer, which isolated the change.

The general principle of layering is that the upper layer can call the underlying function directly, and the lower level cannot call the upper function directly. In reality, the lower layer often calls the upper function in turn. For example, when you copy a file, you call a copy file function at the interface layer. The interface layer is the upper level, the Copy file function is lower, the upper layer calls the lower class, of course. But if you want to update the progress bar when copying files, the problem comes. On the one hand, only the copy file function knows the progress of the copy, but it cannot update the progress bar of the interface. On the other hand, the interface knows how to update the progress bar, but it doesn't know the progress of the copy. What to do. The common practice is to set a callback function to the Copy file function, copy file function in the appropriate time call this callback function to notify the interface update state.

related to abstraction. abstraction is one of the most important concepts in object-oriented, and it is also a powerful place for object-oriented. Object-oriented is just a thought, as we all know, with C language can achieve object-oriented programming. This is not for the sake of fashion, but a practical method. If you doubt this, look at the Open-source code, such as GTK +, Linux kernel, and so on.

Interfaces are the most advanced abstractions. In Linux kernel, the concept of interface is ubiquitous, like a virtual file system (VFS), which defines the interface of a file system, and as long as you follow the specification of this interface, you can develop a file system on your own to hang up. Device drivers are more so, and different device drivers have their own set of different interface specifications. When you develop your own device to develop a driver, just follow the appropriate interface specification. How the interface is represented in the C language. It's simple, it's a set of function pointers.

is related to the interface being separated from the implementation. Programming for interfaces, rather than programming for implementation, is the first design guideline for design patterns. The goal of separating interfaces from implementation is to isolate changes. Software is a change, and if you can't isolate the things that are changing and cause far-reaching, the price is huge. This is not what everyone wants to see.

Since the C language can realize object-oriented programming, the design pattern can be used to separate interface and implement. Like bridging mode, policy mode, state mode, proxy mode, etc., in C, none of them need to be implemented using function pointers.

is related to the principle of loose coupling . One of the reasons why the process is less than the object-oriented is that it does not map the real model to the computer visually, as object-oriented. Process-oriented is a layer of control, and object-oriented more emphasis on the division of the division between the objects. The objects in the real world are in the lower level relationship and are in the majority of the reciprocal relationship. In other words, the interaction between objects is often bidirectional. This strengthens the coupling between objects.

Coupling itself is not wrong, in fact coupling is essential, there is no coupling without collaboration, objects can not form a whole, nothing can do. The key is that the coupling is appropriate, and the coupling should be as loose as possible under the premise of realizing the predetermined function. In this way, some of the changes in the system will have little effect on the other parts.

function pointers are the best tool for decoupling object relationships. Signal (such as the Signal in Boost's Signal and glib) is a typical example of an object whose state may be changing (or triggering events), while other objects are concerned about its changes. Once the object has changed, the other object performs the appropriate action.

If the object directly invokes the function of the other object, the function is complete, but the coupling between the objects is too tight. How to minimize this coupling, signal mechanism is a good way. It works as follows: Other objects that are concerned with the object's changes actively register a callback function into the object. Once the object has changed, these callback functions are called to notify other objects. Functions are also implemented, but the coupling between them is reduced.

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.