><FONT face=宋体 color=#113dee>在C++ BUILDER中,事件是一种委托模型,它是对消息的封装。如果你用过VC,你就知道在VC中并不存在什么事件,而只有消息处理函数,而在C++ BUILDER中则是由事件处理函数来负责响应消息。同是,事件本身也是一指针,它是一个闭包,通常在C++ BUILDER中有两种事件:通知类型事件(即TNOTIFYEVENT,对WINDOWS消息的封装)和自定义事件。除此之外,我们也知道事件是通过一个虚拟函数来点燃的,比如说ONEXIT事件是由DOEXIT虚拟函数点燃的,下面我写一个自定义事件,很明显以下我写的代码中的事件将是对WM_MYMESSAGE消息的封装.
><FONT face=宋体 color=#113dee>class TMyControl:public TWinControl
><FONT face=宋体 color=#113dee>.cpp File</FONT></P>
><FONT face=宋体 color=#113dee>//Omiting constructor and deconstructor# H4 [" j; t% G+ O3 y
oMyEvent(Param1,.....)
oSomething6 x2 s B& P" _" ?6 S
><FONT face=宋体 color=#113dee></FONT> </P>
><FONT face=宋体 color=#113dee>__closure是一个很奇妙的东西!很不错,很可惜,VC不支持!目前也只有Borland的编译器支持.</FONT></P>
><FONT color=#113dee>__closure是一个很奇妙的东西!很不错,很可惜,VC不支持!目前也只有Borland的编译器支持</FONT></P><
><FONT color=#113dee>能不能说说奇妙之处呀!</FONT></P>
>说一个不是太标准的吧,他可以当成一个函数指针来使用.</P><
>我就拿C++Builder里的帮助给你看看吧.</P><
>__closure</P><
>The __closure keyword is used to declare a special type of pointer to a member function. In standard C++, the only way to get a pointer to a member function is to use the fully qualified member name, as shown in the following example: </P><
>class base</P><
>{5 C8 G; y3 s- c
>However, you cannot assign a pointer to a member of a derived class to a pointer to a member of a base class. This rule (called contravariance) is illustrated in the following example:</P><
>class derived: public base</P><
>{
>The __closure keyword extension allows you to skirt this limitation, and more. Using a closure, you can get a pointer to member function for an object (i.e. a particular instance of a class). The object can be any object, regardless of its inheritance hierarchy. The object抯 this pointer is automatically used when calling the member function through the closure. The following example shows how to declare and use a closure. The base and derived classes provided earlier are assumed to be defined.</P><
>int main(int argc, char* argv[])</P><
>{' {6 I9 {) ?3 p4 A- @6 @
>Closures also work with pointers to objects, as illustrated in this example:</P><
>void func1(base *pObj)</P><
>{, F8 t* @) n: N" g7 f
> // Initialize the closure.</P><
> myClosure = pObj->func;</P><
> // Use the closure to call the member function.</P><
> myClosure(1);
>int main(int argc, char* argv[])</P><
>{ m% J) B% z; F
>Notice that we are passing a pointer to an instance of the derived class, and we are using it to get a pointer to a member function in the base class - something standard C++ does not allow us to do.* `. f0 E: h$ f' E% {| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |