Further common
The ERROR_NOTIFY_EVENT type defined above is only available for class B, which is not compatible with other classes, but other classes may require the same error notification service, so it is necessary to make it a versatility, and can achieve this with C , some people The function mode may be thought of because the function template is not a C type, so it cannot be instantiated, and the class template can specify the type of instantiation. The next type of template has enabled the package of the callback function pointer type.
Template
{
Private:
T * Object;
Typedef void (int notify_code); // Defines member function pointer type
Event_type method;
PUBLIC:
CNotifyEvent (T * Object, Event_Type Method)
{
This-> Object = Object;
This-> method = method;
}
Int donotify ()
{
(Object -> * method) (); // trigger a callback, performing event notifications.
}
}
Below is the example code code in the VC:
Class CsourcesClass;
Class CTargetClass;
Class csourceclass: public cobject // This is a class that triggers event notification
{
Private:
CNotifyEvent
protected:
voiddonotify ()
{
IF (m_onnotify) m_onnotify-> donotify (); // Trigger event notification
}
PUBLIC:
CSourceClass ()
{
m_onnotify = NULL;
}
~ Csourceclass ()
{
}
// Set the event notification attribute via setnoityEvent
Void setnoityEvent (CNotifyEvent
{
m_onnotify = Event_sink;
}
}
Class CTargetClass: public cobject // This is a class that requires event notification
{
Private:
CNotifyEvent
protected:
Void freenotifyeventsink ()
{
IF (m_callback) {
Delete m_callback;
m_callback = NULL;
}
}
Void newNotifyEventSink () // Create a new event notification class template instance
{
M_Callback = New CNOTIFYEVENT
this, / / indicate an instance of objects that need to be called the event notification
& CtargetClass :: removeNotify // Indicates class members function types for callback events
);
}
PUBLIC:
CTargetClass ()
{
NewNotifyEventsink ();
}
~ CtargetClass ()
{
FreeNotifyEventsink ();
}
Void ReceiveNotify (int notify_code)
{
// Treat event notification
}
CNotifyEvent
{
Return m_callback;}
}
CsourceClass SourceObj;
CTARGETCLASS TARGETOBJ;
SourceObj.setnoityEvent (targetobj.getNotiSink ()); // Event Attribute assignment
It can be seen that after a generic transformation of the class template, CNOTIFYEVENT can be applied to different class instances. Various types of event callback types can be defined by changing the parameter structure of Event_Type in CNotifyEvent.
summary
This paper introduces a method of comparing mutual callback between class instances in C . This method mainly uses the following characteristics of C :
1. Member pointer, this is a characteristic of C , is another reflection of the high flexibility in C ; the purpose of using member pointers is to implement the member function of the reference object instance, so that other classes directly call a member function of an object instance. may. The VCL is based on this implementation of the event callback mechanism.
2. Template Template, the purpose of using the template is to enable the defined event callback mechanism to have a universal type, which can be targeted for different class types, which is equivalent to the type of event defined in the VCL, is actually a function pointer type, in C to achieve The best selection of versatility uses templates (generic).
Applying this method to make interactions between classes and classes more convenient and intuitive. When writing your own class libraries, you can use this method to build a class interaction framework for class libraries, and improve the ease of use of class libraries.
references
VC 6.0 Help Document
"C efficient programming"
2004-03-02