In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of C and pointers for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The examples in this article have two concepts: Task and Executor. The task has a name (taskName) and can be executed (execute). The executor has nothing to do with the content of the specific task, but only the implementation method of the callback task, so that our executor can do more general. The task interface only needs to implement an execute method, so our tasks can be varied and can be executed by the executor through a unified interface set. This is not only the basic idea of object-oriented, but also a common way of abstraction. Let's take a look at the examples.
As you can imagine, the main function looks something like this:
Int main (int argc, char** argv) {Task * T1 = TaskConstruction ("Task1", run); / / run here is a function pointer Executor * exe = ExecutorConstruction (); exe- > setTask (T1); exe- > begin (); exe- > cancel (); Task * T2 = TaskConstruction ("Task2", run2); / / run2 here is also a function pointer to construct a Task.exe- > setTask (T2); exe- > begin (); exe- > cancel (); return (EXIT_SUCCESS);}
The running result is:
Task: [Task1] is ready to run [a = 1.200000, b = 2.300000] [(a + b) * (a-b) =-3.850000] cancel is invoked heretask: [Task2] is ready to runanother type of execute,just print out some informationcancel is invoked here
All right, let's take a closer look at the implementation:
Define Interfac
First, define the interfaces for Task and Executor entities:
For the Task interface, note the _ this field. This pointer plays an important role in hold the entire Task instance. Then there is a string of taskName, and a function pointer that is passed in when initializing (constructing) the Task. This execute () function is interesting, it is not used internally, but is executed by the executor callback.
# ifndef _ ITASK_H#define _ ITASK_Htypedef struct Task {struct Task * _ this;char * taskName;void (* execute) ();} Task;void execute (); # endif / * _ ITASK_H * /
The executor interface is a little more complex than the Task interface, which contains a _ this pointer and a reference to Task, followed by external interfaces begin () and cancel (). For the consumer of the interface, they only need to call setTask () on the interface instance, pass the task to the executor, and then call begin () at the appropriate time, waiting for the task to end normally or call cancel () to cancel it.
# include "ITask.h"
# ifndef _ IEXECUTOR_H
# define _ IEXECUTOR_H
Typedef struct Executor {
Struct Executor * _ this
Task * task
Char * (* setTask) (Task* task)
Void (* begin) ()
Void (* cancel) ()
} Executor
Char * setTask (Task * task)
Void begin ()
Void cancel ()
# endif / * _ IEXECUTOR_H * /
Implementation interface
# include
The implementation of the executor is a little more complex. When constructing, the function pointer is set internally and the function that needs to be executed is dynamically executed when external calls are made. This sentence may be somewhat roundabout. Look at it this way: when constructing Executor, executor- > begin = begin This statement registers the following implementation of void begin () in the structure, but it is not clear what to execute. After setTask, the address of the callback function is clear:
(executor- > _ this- > task = task;), call begin () at this time to correctly call to the registered Task. # include
In fact, the code of both implementations is not complex, and if you have a better understanding of C pointers, there will be no problem.
Using OO in C #
In order to experiment, we might as well design two different Task, one Task is one of the four mixed operations that calculate two numbers, and the other is just used to print a little information. Then we can see that they use exactly the same interface to execute:
# include
Then, when they win the prize in Main, they register with Task with the code as follows:
# include
This is the end of this article on "sample Analysis of C and pointers". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.