<>说一个不是太标准的吧,他可以当成一个函数指针来使用.</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><>{1 w2 Y8 Z! h- ?/ ^( ? C" S6 y+ z
public:0 ^" u6 R* L$ P8 c I
void func(int x) { };8 _3 A( t4 R* k- e) H$ G4 e4 @
}; . l+ B) ?; z7 o2 k' Q* itypedef void (base::* pBaseMember)(int); " l* q8 i: [ _int main(int argc, char* argv[])5 K6 I; i3 j1 q+ H8 {5 i
{ ( @6 S5 r9 u$ G+ J$ J/ S' C base baseObject; . ?: X0 T+ x! c pBaseMember m = &base::func; // Get pointer to member 'func' 3 C l& S" U( U7 T; e' S+ g // Call 'func' through the pointer to member U" \' n n: c% y; t& J( p+ o
(baseObject.*m)(17); : i& u0 @$ ~2 R( f return 0; # R0 L4 W! a" s* Q9 S; J}</P><>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><>{ 9 w+ a8 A- H7 X public: ) O0 j* o8 R( b" N4 l) I0 R& [& T1 t void new_func(int i) { };$ b L3 U% \$ V3 I+ e* D: Y
};8 d& s$ s% w# o4 u( A4 d# w% T
int main(int argc, char* argv[]) " V. F, E8 l9 }0 u# `0 ]{& `/ j) I4 |- U# y
derived derivedObject;( K4 f, h; y2 I' i
pBaseMember m = &derived::new_func; // ILLEGAL8 q5 Q! L, a1 l0 K1 s5 V6 p6 F& G" H
return 0;+ ^( Y0 g5 x$ U6 ]$ F
}</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><>{) `/ x% X6 P/ ?3 V4 J) K
derived derivedObject; - w- C6 p3 c: T7 A' `1 Z. P/ c void (__closure *derivedClosure)(int); 2 g+ n x5 `+ U. z3 j B, a4 x derivedClosure = derivedObject.new_func; // Get a pointer to the 'new_func' member. 0 {, ?/ g3 K z8 r X- x! S // Note the closure is associated with the 5 R! p! j& [$ |! L- n0 R$ y // particular object, 'derivedObject'. 8 J' E/ G8 E0 G2 o. u% t derivedClosure(3); // Call 'new_func' through the closure.6 M5 t& H+ U; p, i6 [4 z9 \
return 0; 0 n% H% ]" C7 y+ x- t}</P><>Closures also work with pointers to objects, as illustrated in this example:</P><>void func1(base *pObj)</P><>{& d6 t: A# z7 k& \ o
// A closure taking an int argument and returning void./ U* `2 ]' h/ X- ^* y
void ( __closure *myClosure )(int);</P><> // Initialize the closure.</P><> myClosure = pObj->func;</P><> // Use the closure to call the member function.</P><> myClosure(1);% N, W1 p p* F' t' Y
return; 5 L& s/ a b1 `: R6 p( b+ h}</P><>int main(int argc, char* argv[])</P><>{ $ r+ K4 f$ [5 e derived derivedObject;6 O* s; W" q3 F, ~1 o- |% k
void (__closure *derivedClosure)(int); ; h1 L5 n1 B. X+ w1 K- c; I derivedClosure = derivedObject.new_func; // Same as before...7 Y* Y$ H+ N j; `; W4 y
derivedClosure(3); 0 |0 J" S N- z( d* |0 c
// We can use pointers to initialize a closure, too. ) |# @& _3 t4 R Y4 L+ A // We can also get a pointer to the 'func' member function 2 T6 m$ E' _) C; A& l# k7 {! k. B# o; D // in the base class. ) f- m6 Q7 l! b2 s5 _) ]4 p func1(&derivedObject);5 a6 X5 J7 Z, {5 O( f/ a9 Z
return 0;. s8 h1 v9 t. c3 P2 O+ g/ _# I7 j6 d
}</P><>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.$ c% C6 D( Q, e4 h
Closures are a key part of the C++ Builder RAD environment. They give us the ability to assign an event handler in the Object Inspector. For example, a TButton object has an event called OnClick. In the TButton class, the OnClick event is a property that uses the __closure keyword extension in its declaration. The __closure keyword allows us to assign a member function of another class (typically a member function in a TForm object) to the property. When you place a TButton object on a form, and then create a handler for the button抯 OnClick event, C++ Builder creates a member function in the button抯 TForm parent, and assigns that member function to the OnClick event of TButton. This way, the event handler is associated with that particular instance of TButton, and no other. </P>