Let C ++ also support events

zhaozj2021-02-16  93

Let C also support events?

(http://blog.9cbs.net/wqf2/)

Object-oriented development to today, people are not satisfied with the description of a thing only stay at properties and methods. 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:

#include

Using namespace std;

/ / Define the type of event function

TYPEDEF VOID (* EONBEFOREINVOKE) ();

Typedef void (* eonafterInvoke) ();

// A class that has an event

Class CsomeObject1

{

PUBLIC:

/ / Define events

EonbeforeInvoke OnbeforeInvoke; // Call an event

EonafterInvoke onfterInvoke; // After calling events

PUBLIC:

CsomeObject1 ()

{

OnbeforeInvoke = null;

ONAFTERINVOKE = NULL;

}

Void invoke ()

{

/ / Trigger OnBeforeInvoke Event

IF (OnbeforeInvoke! = NULL)

OnbeforeInvoke ();

Cout << "csomeObject1 :: invoke () << endl;

// Trigger ONAFTERINVOKE event

IF (onfterInvoke! = null)

ONAFTERINVOKE ();

}

}

// csomeObject1 creator of the object

Class CEVENTST1

{

CsomeObject1 m_objsome;

Static void OnbeforeInvoke ()

{

Cout << "before calling ..." << Endl;

}

Static void onfterInvoke ()

{

Cout << "After calling ..." << Endl;

}

// 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 ()

{

INSTALLEVENTS ();

}

~ CEVENTTEST1 ()

{

Uninstallevents ();

}

Void invoke ()

{

m_objsome.invoke ();

}

}

INT Main (int Argc, char * argv []) {

CEVENTTEST1 TEST1;

TEST1.INVOKE ();

}

I remember that when I was programmed under TurboC, although there was no C , it is still written to the code to the object, which is to implement the Class of C in the way to add a function pointer domain in Struct. Today's function pointers are still useful.

2. In the form of virtual functions, the code is as follows:

#include

Using namespace std;

// Event interface

Class __Declspec (novTable) ISOMEOBJECT2EVENTS

{

Friend class csomeObject2;

protected:

Virtual void OnbeforeInvoke ();

Virtual void onfterInvoke ();

}

// A class that has an event

Class CsomeObject2

{

PUBLIC:

IsomeObject2events * m_pevents;

Void invoke ()

{

/ / Trigger OnBeforeInvoke Event

IF (m_pevents! = NULL)

M_PEVENTS-> OnbeforeInvoke ();

Cout << "csomeObject2 :: invoke () << Endl;

// Trigger ONAFTERINVOKE event

IF (m_pevents! = NULL)

m_pevents-> onfterInvoke ();

}

}

// csomeObject2 category of objects

Class CEVENTTEST2: PUBLIC ISOMEOBJECT2EVENTS

{

CsomeObject2 m_objsome;

// Call the event processing

Virtual void OnbeforeInvoke ()

{

Cout << "before calling ..." << Endl;

}

// After calling the event processing

Virtual void onfterInvoke ()

{

Cout << "After calling ..." << Endl;

}

// Installation event

Void Installevents ()

{

m_objsome.m_pevents = this;

}

// Uninstall event

Void Uninstallevents ()

{

m_objsome.m_pevents = NULL;

}

PUBLIC:

CEVENTTEST2 ()

{

INSTALLEVENTS ();

}

~ CEVENTTEST2 ()

{

Uninstallevents ();

}

Void invoke ()

{

m_objsome.invoke ();

}

}

Int main (int Argc, char * argv [])

{

CEVENTTEST2 TEST2;

Test2.invoke ();

}

These two ways have various advantages and disadvantages, the benefits of using the function pointer are high-efficiency high efficiency. The advantage of using the virtual function is that the code is even more clear. Everyone can choose from preferences. If you have any advice or supplements after you have seen it, please don't regain your thoughts and text.

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

New Post(0)