call by refcrence |
答題得分者是:william
|
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
引言: [quote]function ShowF1(i_sqlstr,i_db,i_key:string;Ds :TDataSource );/quote] All parameters are passed by value. In fact, Ds will be passed as a pointer and a copy of it will be made for the function call.既然都是call by value傳入,為什麼在function修改會改變Ds的值?? call by value的定義不是就是在把原來的copy一份傳入,所以function內自己有一份i_sqlStr,改變這個值並不會影響外面的值,可是Ds只在function指定值就可以改變??? |
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Let us do an experiement then... Result is Label2 has not been changed. And I think you mis-understand ccchen... 1) function test(var ok:TObject):Boolean;
2) function test(ok:TObject):Boolean; The above 2 functions will both pass a 4 bytes pointer, not the whole object and there is not way to pass a whole object...
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } procedure Test(ALabel: TLabel); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var ALabel : TLabel; begin ALabel := Label1; Test(ALabel); ALabel.Caption := 'Button'; end; procedure TForm1.Test(ALabel: TLabel); begin ALabel.Caption := 'Test'; ALabel := Label2; end; end. |
ccchen
版主 發表:61 回覆:940 積分:1394 註冊:2002-04-15 發送簡訊給我 |
引言: function ShowF1(i_sqlstr,i_db,i_key:string;Ds :TDataSource ); 請問是不是如果function 的傳入值是物件的話,代表的意思是 call by reference 不是 call by value 而i_sqlstr 這些string就是call by value 不知以上觀念是否有誤以上的說法是正確的 補充一下: Delphi的object是很特殊的, 雖然實際上都是pointer, 由於call by address會使程式寫起來較麻煩, 老是要用@,^等operator. Delphi很體貼的讓我們可以直接使用, 而不用像C 老是須考慮用.還是-> 這就比較像是call by reference, 不完全是call by address 其實這只是名詞問題, 你只要知道object永遠是傳4 byte的pointer, 此pointer指向某物件之位址(如William兄所說)就沒錯了. 當然若你要寫C 時, call by value, call by address, call by reference就須弄清楚了 |
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
綜合以上兩位所講
ccchen前輩說這就比較像是call by reference, 不完全是call by address
william前輩說是call by value
兩位都說是以pointer傳遞
我實作william的例子是不會改變
但是ccchen說 "都是call by reference, 不用var, 在函數中更改其值會反應至呼叫者"
我也實作一個範例
procedure TForm1.Button1Click(Sender: TObject);
var i_sqlstr:string;
ALabel : TLabel;
begin
DBGrid1.DataSource:=TDatasource.Create(nil );
i_sqlstr:='select * from ac';
ShowF1(i_sqlstr,'wdata','key1',DBGrid1.DataSource);
end;
procedure ShowF1(i_sqlstr,i_db,i_key:string;Ds :TDataSource );
var qry :TQuery;
begin
qry:=TQuery.Create(nil );
qry.SQL.Clear ;
qry.SQL.Add(i_sqlstr);
qry.DatabaseName :=i_db ;
qry.Open ;
Ds.DataSet:=qry;
end;
真的是指定Ds後直接反應在呼叫者,所以哪我模糊了,不知可否解釋一下這兩個範例有何差別???
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Let me try to explain...
function test1(var ok:TObject):Boolean; function test2(ok:TObject):Boolean; AObject := TObject.Creae; { e.g. @AObject = $ABCDEF AObject^ = $123456 } test1(AObject); { the actual pointer @ $ABCDEF is passed } test2(AObject); { a copy of the pointer value $123456 is copied to a new location, e.g. $CCCCCC, and it is the poitner @CCCCCC is being passed } |
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
我明白william的意思
可以請william前輩指點一下,Ds 是以call by value傳入
那為什麼在function指定Ds的值,呼叫方會有所改變
不是應該DS指定成什麼,都不會影響到DBGrid1.DataSource
procedure TForm1.Button1Click(Sender: TObject);
var i_sqlstr:string;
begin
DBGrid1.DataSource:=TDatasource.Create(nil );
i_sqlstr:='select * from ac';
ShowF1(i_sqlstr,'wdata','key1',DBGrid1.DataSource);
end;
procedure ShowF1(i_sqlstr,i_db,i_key:string;Ds :TDataSource );
var qry :TQuery;
begin
qry:=TQuery.Create(nil );
qry.SQL.Clear ;
qry.SQL.Add(i_sqlstr);
qry.DatabaseName :=i_db ;
qry.Open ;
Ds.DataSet:=qry;
end; 發表人 - cyl 於 2003/01/14 17:41:26
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Don't know if you could understand what I would like to express
<>< face="Verdana, Arial, Helvetica">引言:我明白william的意思
可以請william前輩指點一下,Ds 是以call by value傳入
那為什麼在function指定值,回傳回呼叫方
procedure TForm1.Button1Click(Sender: TObject); var i_sqlstr:string; begin DBGrid1.DataSource:=TDatasource.Create(nil); {e.g. DBGrid1.DataSource: address=$aaaa, value=$1111} i_sqlstr:='select * from ac'; ShowF1(i_sqlstr,'wdata','key1',DBGrid1.DataSource); end; procedure ShowF1(i_sqlstr,i_db,i_key:string;Ds :TDataSource ); {e.g. Ds: address=$bbbb, value=$1111 } var qry :TQuery; begin qry:=TQuery.Create(nil ); qry.SQL.Clear ; qry.SQL.Add(i_sqlstr); qry.DatabaseName :=i_db ; qry.Open ; Ds.DataSet:=qry; {@Ds.DataSet = @DBGrid1.DataSource here } end; |
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
procedure ShowF1(i_sqlstr,i_db,i_key:string;Ds :TDataSource );
{e.g. Ds: address=$bbbb, value=$1111 }
var qry :TQuery;
begin
qry:=TQuery.Create(nil );
qry.SQL.Clear ;
qry.SQL.Add(i_sqlstr);
qry.DatabaseName :=i_db ;
qry.Open ;
Ds.DataSet:=qry;
{@Ds.DataSet = @DBGrid1.DataSource here } -->這句不懂
end; 分別指向不同的address.所以Ds改變內容,並不影響DBGrid1.DataSource?? 發表人 - cyl 於 2003/01/14 18:10:49
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言:{@Ds.DataSet = @DBGrid1.DataSource here } -->這句不懂 cyl 於 2003/01/14 18:10:49typed wrongly... I means Ds.DataSet is the same as DBGrid.DataSource.DataSet. Assume: 1) DBGrid1.DataSource: address=$aaaa, value=$1111 2) Ds := DBGrid1.DataSource (i.e. @Ds=$bbbb Ds^=$1111) Then you may think Ds.DataSet as Ds^.DataSet and hence Ds.DataSet=DBGrid1.DataSource^.DataSet |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |