線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1628
推到 Plurk!
推到 Facebook!

指標轉型一問

尚未結案
BGman
初階會員


發表:28
回覆:85
積分:42
註冊:2003-01-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-02-19 17:02:33 IP:211.22.xxx.xxx 未訂閱
我有兩個問題,問題一: 在C 中, int *pI; 轉型方法為 (double*)pI 在Delphi 中,pI = ^integer; 請問指標要如何轉成double? 問題二: 在處理ComboBox 的AddObject 時,我必須寫一個繼承 TObject 的類別 然後將我需要的資料格式,寫入Class Member中,是不是只有這個辦法?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-02-19 17:40:54 IP:147.8.xxx.xxx 未訂閱
引言: 我有兩個問題,問題一: 在C 中, int *pI; 轉型方法為 (double*)pI 在Delphi 中,pI = ^integer; 請問指標要如何轉成double? PDouble(pointer(PI))^.. are you sure? or you want to typecast and integer to double, e.g. double(pI^) 問題二: 在處理ComboBox 的AddObject 時,我必須寫一個繼承 TObject 的類別 然後將我需要的資料格式,寫入Class Member中,是不是只有這個辦法? no... e.g. Items.AddObject('123',pointer(2))
BGman
初階會員


發表:28
回覆:85
積分:42
註冊:2003-01-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-02-21 16:57:29 IP:211.22.xxx.xxx 未訂閱
原來可以這樣用,果然厲害!但是還有一點小疑問...
type
  TRec = Record
  x : integer;
  y : integer;
  end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyRec :^TRec;
begin
  new(MyRec);
  MyRec.x := 5; 為何不是? MyRec.x^ := 5;
  MyRec.y := 10;為何不是? MyRec.y^ := 10;
  ComboBox1.Items.AddObject('NO1',pointer(MyRec));
end;    procedure TForm1.ComboBox1Change(Sender: TObject);
var
MyRec :^TRec;
begin
  MyRec := pointer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
  ShowMessage(IntToStr(MyRec.x));
end;
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-02-21 17:52:41 IP:147.8.xxx.xxx 未訂閱
Delphi is too nice and you can ignore the ^ in some cases,e.g.
PRec = ^TRec;
TRec = record
    A: integer;
end;    var
    ARec: PRec;
.....
ARec^.A := 1; { can be written as ARec.A := 1;}
IMHO, better keep the '^'. It make it clear it is a pointer to some records, not an object. <>< face="Verdana, Arial, Helvetica">引言: 原來可以這樣用,果然厲害!但是還有一點小疑問...
type
  TRec = Record
  x : integer;
  y : integer;
  end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyRec :^TRec;
begin
  new(MyRec);
  MyRec.x := 5;  == MyRec^.x := 5;
  MyRec.y := 10; == MyRec^.y := 10;
  ComboBox1.Items.AddObject('NO1',pointer(MyRec));
end;    procedure TForm1.ComboBox1Change(Sender: TObject);
var
MyRec :^TRec;
begin
  MyRec := pointer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
  ShowMessage(IntToStr(MyRec.x));
end;
系統時間:2024-05-05 6:17:15
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!