[Repost] Let C ++ also support events

zhaozj2021-02-16  95

Repost [http://blog.9cbs.net/glock18/archive/2004/07/14/41566.aspx] Object-oriented development to today, people are not satisfied with the description of a thing only stay in attributes and methods only . Events are also adopted by the basic components of the object, adopted by an emerging object-oriented language. The so-called event is an object that triggers a notification object under a particular condition, which is processed by the creator. We can see the real handling process of the event is not packaged in the object. Support for events is provided in languages ​​such as Java, C #, DEPHI, or in the form of a keyword, or in the form of a library. No C can not see any keywords and class libraries about the events, is C can't have this characteristic? As long as you move my heart, there is no difficulty. Now let's let C support events. This is only considered on the language without using any platform-related characteristics (messages, event objects ...). So I took out two implementation methods, one is to use a function pointer and another use virtual function. 1. Use the function pointer mode, the code is as follows:

// define event type of the function typedef void (* EOnBeforeInvoke) (); typedef void (* EOnAfterInvoke) (); // event class has a class CSomeObject1 {public: // define event EOnBeforeInvoke OnBeforeInvoke; // called before the event EOnAfterInvoke OnAfterInvoke; // call event public: CSomeObject1 () {OnBeforeInvoke = NULL; OnAfterInvoke = NULL;} void invoke () {// trigger OnBeforeInvoke event if (OnBeforeInvoke = NULL!) OnBeforeInvoke (); cout << "CSomeObject1 :: INVOKE () "<

// Trigger OnAfterInvoke event if (onfterInvoke! = Null) onafterInvoke ();}};

// CsomeObject1 Objects Class Class CEVENTTEST1 {CsomeObject1 m_objsome

Static void OnbeforeInvoke () {cout << "Before ..." <} static void onfterInvoke () {cout << "After call ..." <} // Installation Event Void Installevents () {m_objsome.onbeforeinvoke = OnbeforeInvoke ; M_objsome.onafterInvoke = onfterinvoke;} // Uninstall event void uninstallevents () {m_objsome.onbeforeinvoke = null; m_objsome.onafterInvoke = null;}

PUBLIC: CEVENTTEST1 ()} ~ CEVENTST1 () {UNINSTALLEVENTS ();

Void invoke () {m_objsome.invoke ();}}; int main (int Argc, char * argv []) {CEVENTTEST1 TEST1; TEST1.INVOKE ();} Remember when it is programmed under Turboc, although there is no C , You can still write the code of the object to the object to achieve the Class of C in the way to add a function pointer domain in the Struct. Today's function pointers are still useful. 2. In the form of virtual functions, the code is as follows:

// Event interface class __declspec (novTable) isomeObject2events {Friend Class CsomeObject2; protected: virtual void onbeforeInvoke (); Virtual void onfterInvoke ();};

// event class has a class CSomeObject2 {public: ISomeObject2Events * m_pEvents; void Invoke () {// trigger OnBeforeInvoke event if (m_pEvents = NULL!) M_pEvents-> OnBeforeInvoke (); cout << "CSomeObject2 :: Invoke () " onfterInvoke ();}}

// csomeObject2 Objects Cemented Class CEVENTTEST2: Public IsomeObject2events {

CsomeObject2 m_objsome; // Call before the event handled Virtual void onbeforeinvoke () {cout << "Before ..." <}; // After the event handles Virtual void onafterInvoke () {cout << "After calling ..." <};

// Installation Event Void Installevent () {m_objsome.m_pevents = this;} // Uninstall Event Void Uninstallevent () {m_objsome.m_pevents = null;}

Public: CEVENTTEST2 () {Installevent ();} ~ ceventtest2 () {uninstallevent ();

Void invoke () {m_objsome.invoke ();

}

INT main (int Argc, char * argv []) {CEVENTST2 TEST2; TEST2.INVOKE ();

转载请注明原文地址:https://www.9cbs.com/read-10586.html

New Post(0)