Form的this 傳給其它class使用 |
缺席
|
raistlinmiao
一般會員 發表:17 回覆:20 積分:12 註冊:2004-10-22 發送簡訊給我 |
各位好:
請教一下 我使用一個class 將目前的Form的指標傳給他 然後想由該class來和Form互動 但是將Form的this傳過去執行會有問題(記憶體誤用) 我查不到相關的文章,所以上來請教 請教一下是何原因,謝謝 以下附上code,請各位不吝指教 附件中也是相同的code Unit1.h //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- class Test; class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TButton *Button2; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); private: // User declarations public: // User declarations friend class Test; __fastcall TForm1(TComponent* Owner); Test* a; void Show(){ShowMessage("a");} }; class Test { public: TForm1* Host_Form; void SetForm(TForm1* In_Form); void Speak(); }; void Test::SetForm(TForm1* In_Form) { Host_Form = In_Form; } void Test::Speak() { Host_Form->Show(); } //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif Unit1.cpp //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { a->SetForm(this); //傳下去 請Test class執行 會出錯 a->Speak(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { this->Show(); //直接執行沒問題 } //--------------------------------------------------------------------------- |
papjaj
一般會員 發表:11 回覆:15 積分:5 註冊:2007-01-29 發送簡訊給我 |
我比較土法煉鋼的方式是..
test這個class在宣告void SetForm(TForm1* In_Form)的地方改成 void SetForm(TObject* In_Form); 然後在 setform的這個程式中 將傳進來的form用 (TForm*)In_Form 的方式來使用... 土方法@@ ===================引 用 raistlinmiao 文 章=================== 各位好: 請教一下 我使用一個class 將目前的Form的指標傳給他 然後想由該class來和Form互動 但是將Form的this傳過去執行會有問題(記憶體誤用) 我查不到相關的文章,所以上來請教 請教一下是何原因,謝謝 以下附上code,請各位不吝指教 附件中也是相同的code Unit1.h //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include <forms.hpp><br />//--------------------------------------------------------------------------- class Test; class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TButton *Button2; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); private: // User declarations public: // User declarations friend class Test; __fastcall TForm1(TComponent* Owner); Test* a; void Show(){ShowMessage("a");} };</forms.hpp></stdctrls.hpp> class Test { public: TForm1* Host_Form; void SetForm(TForm1* In_Form); void Speak(); }; void Test::SetForm(TForm1* In_Form) { Host_Form = In_Form; } void Test::Speak() { Host_Form->Show(); } //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif Unit1.cpp //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { a->SetForm(this); //傳下去 請Test class執行 會出錯 a->Speak(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { this->Show(); //直接執行沒問題 } //--------------------------------------------------------------------------- |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |