有關函數指標的問題 |
答題得分者是:Royce520
|
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
請問大家:
下面是程式碼
.h class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); int __fastcall FUN(int a); int __fastcall UseFUN( int (*FP)(int) ,int N); }; .cpp int Test(int a) { return (5*a); } //--------------------------------------------------------------------------- int FunPointer(int (*FP)(int),int Value) { return 2*FP(Value); } //--------------------------------------------------------------------------- int __fastcall TForm1::FUN(int a) { return (a*a*a); } //--------------------------------------------------------------------------- int __fastcall TForm1::UseFUN( int(*FP)(int) ,int M) { return (3*FP(M)); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { ShowMessage(FunPointer(Test,3)); //ok //ShowMessage(UseFUN(FUN,3)); //錯誤 ShowMessage(UseFUN(Test,3)); //ok //ShowMessage(FunPointer(FUN,3)); //錯誤 } //---------------------------------------------------------------------------錯誤的地方該如何修正呢? 小弟經驗尚嫩,若問題很蠢請多多包涵< > 謝謝大家< > 發表人 - taishyang 於 2004/02/11 23:25:50 |
Royce520
高階會員 發表:18 回覆:157 積分:100 註冊:2002-09-13 發送簡訊給我 |
dear taishyang:
一般來說 這是 pointer to memeber functions 的問題,
所以要使用 可以這樣宣告
typedef int __fastcall (MyNotVCLClasses::*FuncPtr)(int); 但是因為 BCB 為他的 VCL components 處理函數 引入了
__closure 這個關鍵字... 所以寫法有了些改變 也多了些好處 typedef int __fastcall (__closure *VCLFuncPtr)(int); 所以, 你必須要在 *.h 中加上
class TForm1; typedef int __fastcall (__closure *VCLFuncPtr)(int); class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); int __fastcall FUN(int a); int __fastcall UseFUN( int (*FP)(int) ,int N); int __fastcall UseFUN2( VCLFuncPtr FP,int N); }; 在 *.cpp 中 int Test(int a) { return (5*a); } int FunPointer(int (*FP)(int),int Value) { return 2*FP(Value); } int __fastcall TForm1::FUN(int a) { return (a*a*a); } int __fastcall TForm1::UseFUN( int(*FP)(int) ,int M) { return (3 * FP(M)); } int __fastcall TForm1::UseFUN2(VCLFuncPtr FP ,int M) { Memo1->Lines->Add(AnsiString().sprintf("in UseFUN2, %X", this)); return (3 * FP(M)); } void __fastcall TForm1::Button1Click(TObject *Sender) { ShowMessage(FunPointer(Test,3)); //ok //ShowMessage(UseFUN2(FUN,3)); // 這樣就可以了 ShowMessage(UseFUN(Test,3)); //ok //ShowMessage(FunPointer(FUN,3)); //錯誤 }ps. 猜想你是要用 UseFUN 來使用 兩種方式的函數指標吧 但就我個人所學 好像是沒有直接的方法的 除非經由 vtable 吧
------
不要忘記呼吸,不要忘記編程! ∩__∩ |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |