全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:725
推到 Plurk!
推到 Facebook!

[問題]請教一下類別變數共用的問題...

答題得分者是:smartboss
abdias
一般會員


發表:5
回覆:4
積分:1
註冊:2004-07-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-01-07 19:59:49 IP:140.127.xxx.xxx 未訂閱
假設我寫了一個母親的類別如下    mother.cpp ----------------------------------- class mother {   public:     int money; };    那我在BCB裡Form1裡寫了以下設定    Unit1.cpp ------------------------------------------- #include  #pragma hdrstop #include "Unit1.h" #include "mother.cpp" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; mother Brother1; //宣告一個哥哥的子類別 //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Brother1.money = "3000"; //告訴他哥哥的零用錢是3000 } 那我想在另一個Form裡宣告一個弟弟的子類別並得到Form1的哥哥類別的money變數,請問除了用extern的方法外,還有什麼方法嗎... Unit2.cpp ---------------------------------------------------------- #include #pragma hdrstop #include "Unit2.h" #include "Unit1.h" #include "mother.cpp" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form2; mother Brother2;//宣告一個弟弟的子類別 //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { //我想在這裡取得哥哥類別的money變數 }
smartboss
初階會員


發表:19
回覆:93
積分:42
註冊:2004-12-29

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-01-08 00:47:42 IP:210.192.xxx.xxx 未訂閱
引言: mother.cpp ----------------------------------- class mother { public: int money; }; #include "Unit1.h" #include "Unit2.h"
abdias 您好:小弟建意您將 #include "mother.cpp" 的層級拉高,同時建意您將class mother 放在mother.h 裡,因為小弟可能是才學粗淺所以第一次看過有人#include .cpp 的檔案,再來分別在您的 "Unit1.h" 裡引入 #include "mother.h" 並在class TForm1 裡宣告 { public: mother Brother1; }; 而在"Unit2.cpp" 裡引入 #include "Unit1.h" void __fastcall TForm2::Button1Click(TObject *Sender) { Form1->Brother1.money <----這就素葛葛的錢了 //我想在這裡取得哥哥類別的money變數 }
abdias
一般會員


發表:5
回覆:4
積分:1
註冊:2004-07-30

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-01-08 10:25:06 IP:140.127.xxx.xxx 未訂閱
哦哦哦...我試出來了啦... 會include .cpp是因為我新增一個cpp檔而不是unit... 也可以說是亂試吧... 總之...謝謝你的回答...
系統時間:2024-06-28 19:32:13
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!