物件中函數參數是另一個函數指標的問題? |
尚未結案
|
hornacek
一般會員 發表:29 回覆:76 積分:21 註冊:2004-02-02 發送簡訊給我 |
程式碼如下:
class A { private: void func(void); void func2(void (*func)(void)); void func3(void); }; void A::func(void) { } void A::func2(void (*func)(void)) { } void A::func3(void) { func2(func); }編譯之後出現 Cannot convert 'void (*(__closure )())()' to 'void (*)()' Type mismatch in parameter 'func' (wanted 'void (*)()', got 'void') 的錯誤?請問我是哪裡用錯了?謝謝! 把func3改成 void A::func3(void) { func2(::func); }編譯後出現 Undefined symbol 'func' 的錯誤訊息?請問這到底是怎麼回事? 我要如何使用函數指標當作函數的參數呢?謝謝! |
pwipwi
版主 發表:68 回覆:629 積分:349 註冊:2004-04-08 發送簡訊給我 |
|
hornacek
一般會員 發表:29 回覆:76 積分:21 註冊:2004-02-02 發送簡訊給我 |
引言: hornacek你好: 參考一下http://delphi.ktop.com.tw/topic.php?TOPIC_ID=56371 不知道能否解決你的問題?謝謝!我的問題解決了!不過不是很清楚為什麼? 只要加上__closure關鍵字,如下: class A { private: void func(void); void func2(void (__closure *func)(void)); void func3(void); }; void A::func(void) { } void A::func2(void (__closure *func)(void)) { } void A::func3(void) { func2(func); }以下是BCB Help對於__closure的說明 __closure The __closure keyword is used to declare a special type of pointer to a member function. Unlike a regular C member function pointer, a closure contains an object pointer. In standard C , you can assign a derived class instance to a base class pointer; however, you cannot assign a derived class? member function to a base class member function pointer. The following code illustrates this: class base { public: void func(int x); }; class derived: public base { public: void new_func(int i); }; void (base::*bptr)(int); bptr = &derived::new_func; // illegalHowever, the __closure language extension allows you to do this in C Builder. A closure associates a pointer to a member function with a pointer to a class instance. The pointer to the class instance is used as the this pointer when calling the associated member function. A closure declaration is the same as a function pointer declaration but with the addition of the __closure keyword before the identifier being defined. For example: struct MyObject { double MemFunc(int); }; double func1(MyObject *obj) { // A closure taking an int argument and returning double. double ( __closure *myClosure )(int); // Initialize the closure. myClosure = obj -> MemFunc; // Use the closure to call the member function and pass it an int. return myClosure(1); }Although the preceding discussion illustrates closures by assigning descendant class methods, you can assign any class method pointer to a closure. That is, closures allow completely disparate classes to share a method pointer. Closures are used with events in C Builder. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |