請問副程式的OUTPUT問題 |
答題得分者是:jow
|
jenwe
一般會員 發表:34 回覆:38 積分:24 註冊:2007-03-23 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
|
jenwe
一般會員 發表:34 回覆:38 積分:24 註冊:2007-03-23 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
Call By Value, Call By Reference, Call By Address
是指傳遞參數的形式...^_^ [code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- struct CPoint { int X; int Y; }; //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TButton *Button1; TListBox *ListBox1; void __fastcall Button1Click(TObject *Sender); private: int CallByValue(int x, int y); CPoint CallByReference(int &x, int &y); CPoint* CallByAddress(int *x, int *y); public: __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [/code] [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { } //--------------------------------------------------------------------------- int TForm1::CallByValue(int x, int y) { return x y ;//改變的x, y的值不影響叫用端 } //--------------------------------------------------------------------------- CPoint TForm1::CallByReference(int &x, int &y) { CPoint ret;//要回傳的struct ret.X=x ; ret.Y=y ; return ret;//傳入的 x, y值被改變,並傳回叫用端 } //--------------------------------------------------------------------------- CPoint* TForm1::CallByAddress(int *x, int *y) { CPoint *ret = new CPoint;//要回傳的struct, 叫用端要負責delete ret->X=(*x) ; ret->Y=(*y) ; return ret;//傳入的 x, y值被改變,並傳回叫用端 } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString S; int x=10, y=20; //Call By Value int z = CallByValue(x,y); S.sprintf("x=%d, y=%d, z=%d",x,y,z); ListBox1->Items->Add(S); ListBox1->Items->Add(""); //Call By Reference CPoint p=CallByReference(x, y); S.sprintf("x=%d, y=%d, p.X=%d, p.Y=%d",x,y,p.X,p.Y); ListBox1->Items->Add(S); ListBox1->Items->Add(""); //Call By Address CPoint *Q=CallByAddress(&x, &y); try{ S.sprintf("x=%d, y=%d, Q->X=%d, Q->Y=%d",x,y,Q->X,Q->Y); ListBox1->Items->Add(S); ListBox1->Items->Add(""); } __finally{ if(Q) delete Q;//叫用端要負責delete } } //--------------------------------------------------------------------------- [/code] |
jenwe
一般會員 發表:34 回覆:38 積分:24 註冊:2007-03-23 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |