The following is a class driven by the message I implemented with virtual functions. An object issued by an object can be implemented, and the corresponding operation of the object that can cause other conformity. The condition is that all objects must be generated by the same event drive class (note template parameters) and its derived class. Very simple, very interesting, everyone may wish to try.
-------------------------------------------------- ----------------------------------------
The header file "Evedri.h" contains the definition and implementation of the event drive class template. #ifndef __evedri__h __ # define __evedri__h__ event drive class template definition // template Class Event_Driven {structure obj_list {event_driven * Obj; // itself. Used to call virtual functions. Obj_list * next;} mine; Link list item. Static Obj_list * point; // The head pointer of the list. This linked list stores a pointer to all objects generated by this class and this class. Public: Event_driven (); // Constructor. Used to store its own pointers in the linked list. ~ Event_driven (); // Destructor. Remove itself in the linked list. Void set_message (t, void * = 0); // Send a function of the message. It will send messages to all objects generated by this class and this class. Virtual void get_mes (t, void *); // ※ Message handler. Defined as a virtual function, to let the base class pointer stored in the list can call the function of the party. }; /// Event Drive Class Template Implementation / Link Top Pointer Initial Empty Template Event_Driven : Obj_list * Event_Driven :: Point = 0; Constructor Realization. Template Event_Driven :: Event_Driven () {mine.obj = this; mine.next = 0; if (0 == POINT) Point = & mine; else {for (Obj_list * P = POINT; 0! = P-> Next; P = P-> Next); P-> Next = & mine;}} The destructor is implemented. Template Event_Driven :: ~ Event_Driven () {if (& Mine == Point) Point = mine.next; else {for (Obj_list * p = POINT; 0! = P-> Next; P = P-> Next) {p-> next = mine.next; return;}}} The function of sending messages is implemented.
Template Void Event_Driven :: set_message (t mes_class, void * MES_DATA) {for (obj_list * p = POINT; 0! = P; p = p-> next) P-> Obj-> get_mes (MES_CLASS, MES_DATA);} Message Processing Function Implementation. Template Void Event_Driven :: GET_MES (T Mes_Class, Void * MES_DATA) {} # ENDIF ends. -------------------------------------------------- ----------------------------------------
The following is an example of using an event-driven class.
-------------------------------------------------- ----------------------------------------
#include "Evedri.h" #include Enum event {ms1, mes2}; class aa: virtual public evenet_driven {char * ObjName; public: aa (char * str) {ObjName = Str;} Void get_me (Event I, Void *) {cout << objName << "is caled << endl;}}; aa w (" w "); void main () {aa a (" a "), B ( "b"), c ("c"); a.set_message (ms1);
-------------------------------------------------- -----------------------------------------