Delphi 2009 - Pointer Math (指针数学运算)
|
Type
|
Switch
|
|
Syntax
|
{$POINTERMATH ON} or {$POINTERMATH OFF}
|
|
Default
|
{$POINTERMATH OFF}
|
|
Scope
|
Local
|
在一些精确实例中,指针运算将任意给定的类型化指针当作一个可调整的序数,这样就可以直接对指针变量进行简单的算数运算。同样允许通过使用array[]运算符将指针变量当成无界数组。注意例子中所述,类型数组中索引的增加等同于指向类型的指针的增加。变动指针的增量是以数组元素之字节数作为尺寸大小,而非单个字节。
POINTMATH指令具有局部作用域。即是说,这个指令开关打开后而没有被关闭的情况,它的作用域将延伸到模块的结束处。也就是,在指令开的情况下声明的类型化指针,其所有类型变量是允许可调整指针运算和数组索引的,即使指令关闭亦然,在任何由该指令所包围的代码块中,任何类型化指针均允许作算数运算,不管类型化指针声明期间PointMath指令是否被打开。
该指令只影响类型化指针。而类型指针变量却不允许这样做,这是因为类型指针仅指向void元素,在尺寸上只是0字节。无类型 var或const参数亦不受影响,因为它们根本就不是指针。
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
CheckBox1: TCheckBox;
ListBox1: TListBox;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Rects = ^TRect;//typed pointer
RectList = array[0..10] of TRect; //typed pointer variable
var
Form1: TForm1;
myRects: Rects;
myRectList : RectList;
procedure ListRect(Rect: TRect);
procedure ListRects(PRects: Rects; Count: Integer);
procedure ReadRects;
// procedure WriteRects;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ListRects(myRects, Form1.ComponentCount);
end;
procedure ListRect(Rect: TRect);
begin
Form1.ListBox1.Items.Add(
'BoundsRect: Left: ' + IntToStr(Rect.Left) +
' Right: ' + IntToStr(Rect.Right) +
' Top: ' + IntToStr(Rect.Top) +
' Bottom: ' + IntToStr(Rect.Bottom));
end;
{$POINTERMATH ON}
procedure ListRects(PRects: Rects; Count: Integer);
begin
while Count > 0 do
begin
ListRect(PRects[Count - 1]); // This line will not compile without Tiburon.
// ListRect((PRects + Count - 1)^); // Neither will this line.
Dec(Count);
end;
end;
procedure ReadRects;
var
I, index : Integer;
Temp: TWinControl;
begin
{$T-}
index := 0;
for I := Form1.ComponentCount - 1 downto 0 do
begin
if (Form1.Components[I] is TWinControl) then
begin
Temp := TWinControl(Form1.Components[I]);
myRectList[index] := Temp.BoundsRect;
index := index + 1;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
myRects := Addr(myRectList);//Returns a pointer to a specified object.
ReadRects;
end;
浙公网安备 33010602011771号