In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "C++ programming how to establish a message connection between objects", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C++ programming how to establish a message connection between objects" bar!
I. the data structure of the callback class and its member functions
The CallBack class proposed in this article supports three callback functions. They are: member functions in the callback object, static member functions belonging to the callback class, and ordinary C functions. The CallBackle class contains a callback function table callBackList. It is used to record the event name and a pointer to the callback function and callback object. Each node of the table records the EventRecord for an event. Each event record contains three fields: a pointer to the event name eventName, a pointer to the callback object pointerToCBO, a pointer to the callback function pointerToCBF or pointerToCBSF (where pointerToCBF points to the member function of the callback object, and pointerToCBSF points to the static member function or ordinary function of the callback class. They are in the same body. The callback mechanism provided by the CallBack class is that the callback function in the callback object is registered on the event object; when the event occurs, the event object retrieves and executes the callback function in its callback table. So that the message connection between the two can be established. (for the specific implementation of this class, please refer to the list of programs attached.)
Event name callback object pointer callback function pointer "event" pointerCBOpointerToCBF or pointerTOCBSF
AddCallBack: register the event name and pointer to the callback function and callback object
CallCallBack: in the callback table, retrieve callback functions registered on the specified events and call them
Call the CallCallBack function when the event occurs
The member function that handles the event event
The callback table callBackList, member functions AddCallBack and CallCallBack, inherited from the CallBack class.
PointerToCBO is NULL when the callback function is a static member function or a normal C function.
The event name is the retrieval keyword in the callback table callBackLis.
Other member functions in the callback object
The member function AddCallBack of the CallBack class is used to register the callback function with the callback table of the event object. It has two overloaded versions:
Void CallBack::AddCallBack (char * event,CallBackFunction cbf,CallBack * p); void CallBack::AddCallBack (char * event,CallBackStaticFunction cbsf)
Among them, * AddCallBack is used to register the member functions of a callback object into the callback table of the event object. The second AddCallBack is used to register the static member function of a callback class or a callback class into the callback table of the event object. In the above parameter table, event is the pointer to the event name string, p is the pointer to the callback object, cbf and cbsf are pointers to member functions and static member functions (or ordinary functions), respectively. When the callback function comes from a callback object SomeObject, the transfer member function pointer should be in the following format: (CallBackFunction) & SomeObject::MemberFunctionName; passing a static member function pointer of the SomeObject class should be in the format: (CallBackStaticFunction) & when passing a normal function pointer in the SomeObject::FunctionName; transfer program, you only need to pass the function name.
The member function void CallBack::CallCallBack (char * ename, CallData calldata = NULL) of the CallBack class is used to call all callback functions registered on the event ename. Where calldata is the data pointer (CallData is actually void*, see the program listing). The event object uses it to pass useful data to the callback object. This member function is usually called in the member function of the event object, because usually only the member function of the event object can change the internal data of the object, thus causing some events to occur.
The member function RemoveCallback is used to delete the callback function registered on the event object. Its three overloaded versions are in turn:
Void CallBack::RemoveCallBack (char * event,CallBackFunction cbf,CallBack * p); void CallBack::RemoveCallBack (char * event,CallBackStaticFunction cbsf); void CallBack::RemoveCallBack (char * event)
The parameters such as event,cbf,cbsf,p are the same as those in the member function AddCallBack. * RemoveCallBack is used to delete a member function of a callback object registered on the event event. The second RemoveCallBack is used to delete a normal function registered on the event event or a static member function of a callback class. The third RemoveCallBack removes all callback functions registered on the event event.
2. The usage of CallBack class
Using the CallBack class, you can follow these steps:
1. To determine which objects in the program have relationships, you need to establish a message connection. And determine which object is the event object and which object is the callback object in each specific message connection relationship.
two。 Both the event object class and the callback object class must inherit from the CallBack class to get callback support.
3. Register callback data for the event object. Including: event name, callback function name, pointer to callback object.
4. When the event you are interested in occurs, call the CallCallBack function in the member function that raises the event in the event object class.
Here is a concrete example. Through it you will have a better understanding of how to use the Callback class.
/ / Test program file: test.cpp # include "callback.h" / / "speaker" class class Speaker:public CallBack {private: int volume; public: Speaker (int v): volume (v) {} void IncreaseVolume (int v) / / increase volume member function {volume + = v If (volume > 20) {/ / "Volume greater than 20" event occurs / / calls the callback function CallCallBack ("Volume changed") registered on the two events; CallCallBack ("Volume is greater than 20", & volume);}} void DecreaseVolume (int v) / / reduce the volume member function {volume-= v If (volume < 5) {/ / "Volume less than 5" event occurs / / calls callback function CallCallBack ("Volume changed"); CallCallBack ("Volume less than 5", & volume);}}; / / "ear" class Ear: public CallBack {public: static void Response (CallData callData) / / response to "Volume change" {cout
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.