Post [http://blog.csdn.net/glock18/archive/2004/07/14/42666.aspx]
Since the development of object-oriented technology, people are no longer satisfied with the fact that the description of a thing is only on attributes and methods. Events are also a basic component of objects and are adopted by emerging object-oriented languages. An event is the creator of an object that triggers a notification under a specific condition. The Creator processes the notification object accordingly. We can see that the actual handling process of the event is not packaged in the object. Events are supported in Java, C #, dephi, and other languages, either in the form of keywords or class libraries. C ++ does not see any keywords and class libraries about events. Is C ++ unable to possess the event feature? As long as you use your brains, there is no difficulty. Now let C ++ support events.
Here we only consider the language, rather than using any platform-related features (messages, event objects ...). So I come up with two implementation methods, one is using function pointers, and the other is using virtual functions.
1. Use the function pointer method. The Code is as follows:
// Define the type of the event Function
Typedef void(*Eonbeforeinvoke)();
Typedef void(*Eonafterinvoke)();
// A class with an event
Class csomeobject1
{
Public:
// Define the event
Eonbeforeinvoke onbeforeinvoke;// Events before the call
Eonafterinvoke onafterinvoke;// Post-call event
Public:
Csomeobject1()
{
Onbeforeinvoke=Null;
Onafterinvoke=Null;
}
Void invoke()
{
// Trigger the onbeforeinvoke event
If(Onbeforeinvoke! =Null)
Onbeforeinvoke();
Cout<"Csomeobject1: invoke ()"< // Trigger the onafterinvoke event
If(Onafterinvoke! =Null)
Onafterinvoke();
}
};
// The Creator class of the csomeobject1 object
Class ceventtest1
{
Csomeobject1 m_objsome;
Static void onbeforeinvoke()
{
Cout<"Before calling ..."<}
Static void onafterinvoke()
{
Cout<"After the call ..."<}
// Installation event
Void installevents()
{
M_objsome.Onbeforeinvoke=Onbeforeinvoke;
M_objsome.Onafterinvoke=Onafterinvoke;
}
// Uninstall the 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 programming in turboc++But you can still write object-oriented code, that is, using struct to add function pointer fields to realize the current C++Class. Function pointers are still useful today.
2. Use a virtual function. The Code is as follows:
// Event Interface
Class _ declspec(Novtable)Isomeobject2events
{
Friend class csomeobject2;
Protected:
Virtual void onbeforeinvoke();
Virtual void onafterinvoke();
};
// A class with an event
Class csomeobject2
{
Public:
Isomeobject2events*M_pevents;
Void invoke()
{
// Trigger the onbeforeinvoke event
If(M_pevents! =Null)
M_pevents->Onbeforeinvoke();
Cout<"Csomeobject2: invoke ()"<
// Trigger the onafterinvoke event
If(M_pevents! =Null)
M_pevents->Onafterinvoke();
}
};
// The Creator class of the csomeobject2 object
Class ceventtest2:Public isomeobject2events
{
Csomeobject2 m_objsome;
// Pre-call event processing
Virtual void onbeforeinvoke()
{
Cout<"Before calling ..."<};
// Handle post-call events
Virtual void onafterinvoke()
{
Cout<"After the call ..."<};
// Installation event
Void installevents()
{
M_objsome.M_pevents=This;
}
// Uninstall the 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();
}
The two methods have their own advantages and disadvantages. The advantage of using function pointers is that the event triggering efficiency is high. The advantage of using virtual functions is that the Code makes the activity clearer. You can choose from your preferences. If you have read any comments or comments, do not cherish your thoughts and words.
In essence, Delphi and C # Use the function pointer mechanism to process events. the function pointer is a member of a class. when an event occurs, it calls the corresponding function. however, in languages such as Delphi and C #, the event processing function is anonymous, that is, an object does not know the class of the function that has subscribed to an event, and so on. these methods are called only when an event is triggered. I have tried to achieve similar results in C ++, but it is determined by the Language Characteristics of C ++, it is very difficult to implement such a mechanism in C ++. in the preceding example, "Event" can only point to global functions. A member pointer can point to a member function, but a member pointer requires you to tell the compiler what class of method to point to before assigning a value. in essence, this restricts the use of the "event" feature in C ++. BCB implements this feature, but this is achieved only after a lot of work is done at the compilation layer. the C ++ language does not contain such a feature. in fact, I really appreciate this mechanism in Delphi, which has many advantages. It is several times better than what message ing in MFC.