2012年5月30日 星期三

(BCB)使用指標傳值-1

程式裡面經常可以見到 &變數,尤其在Funtion宣告時裡面常出現
以往只了解是針對記憶體位置做讀寫,實際操作一次

程式功能:
Button1->Caption = Edit1 + Edit2 ;
使用函數 fastcul 計算。


  • 一般常見寫法:

//----------------------------------------------------------------

int TForm1::fastcul(int a, int b)
{
         return (a+b);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        int a ,b ;
        a = StrToInt(Edit1->Text);
        b = StrToInt(Edit2->Text);
        Button1->Caption = fastcul(a,b);
}

//----將值傳到fastcul,計算完成回傳


  • 使用指標寫法
//------------------------------------------------------------------
TForm1 *Form1;
int a ,b;
//------------------------------------------------------------------
bool TForm1::fastcul(AnsiString &TestData)
{
         TestData=a+b;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiString TestData;
        a = StrToInt(Edit1->Text);
        b = StrToInt(Edit2->Text);
        fastcul(TestData);
        Button1->Caption = TestData;
}


以上兩種寫法都可以達到相同要求!