pascal通用指针名称为pointer;有时,将pointer指针称为无类型指针。 注意:object pascal认为每个指针是相异的 如 var a:^integer; b:^integer;//a,b是不能互相替用的 //必须定义一新的变量进行转换 type Myint=^integer; var a,b:Myint;//现在,a,b才相容 //例子 procedure TForm1.Button1Click(Sender: TObject); var pint:^Integer; //整型指针 //pp:pointer; //无类型指针 i:integer; begin i:=10;new(Pint);//分配空间 pint^:=i;Showmessage(inttostr(pint^)); dispose(Pint);//释放空间 end;
定义方法指针Delphi帮助 Procedural types allow you to treat procedures and functions as values that can be assigned to variables or passed to other procedures and functions. For example, suppose you define a function called Calc that takes two integer parameters and returns an integer:function Calc(X,Y: Integer): Integer;You can assign the Calc function to the variable F:var F: function(X,Y: Integer): Integer;F := Calc;If you take any procedure or function heading and remove the identifier after the word procedure or function, what抯 left is the name of a procedural type. You can use such type names directly in variable declarations (as in the example above) or to declare new types:type TIntegerFunction = function: Integer; TProcedure = procedure; TStrProc = procedure(const S: string); TMathFunc = function(X: Double): Double; var F: TIntegerFunction; { F is a parameterless function that returns an integer } Proc: TProcedure; { Proc is a parameterless procedure } SP: TStrProc; { SP is a procedure that takes a string parameter } M: TMathFunc; { M is a function that takes a Double (real) parameter and returns a Double } procedure FuncProc(P: TIntegerFunction); { FuncProc is a procedure whose only parameter is a parameterless integer-valued function }The variables above are all procedure pointers梩hat is, pointers to the address of a procedure or function. If you want to reference a method of an instance object (see Classes and objects), you need to add the words of object to the procedural type name. For exampletype TMethod = procedure of object; TNotifyEvent = procedure(Sender: TObject) of object;These types represent method pointers. A method pointer is really a pair of pointers; the first stores the address of a method, and the second stores a reference to the object the method belongs to. Given the declarationstype TNotifyEvent = procedure(Sender: TObject) of object; TMainForm = class(TForm) procedure ButtonClick(Sender: TObject); ... end; var MainForm: TMainForm; OnClick: TNotifyEventwe could make the following assignment.OnClick := MainForm.ButtonClick;Two procedural types are compatible if they havethe same calling convention, the same return value (or no return value), and the same number of parameters, with identically typed parameters in corresponding positions. (Parameter names do not matter.)Procedure pointer types are always incompatible with method pointer types. The value nil can be assigned to any procedural type. Nested procedures and functions (routines declared within other routines) cannot be used as procedural values, nor can predefined procedures and functions. If you want to use a predefined routine like Length as a procedural value, write a wrapper for it:function FLength(S: string): Integer;begin Result := Length(S); end;Procedural types in statements and expressions
to chenquan(嘉威王子),请你再详细将一下方法指针好吗?我很少看到有人用.还有对象指针,VML,DML指针.你真牛!如果你让我完全把指针搞懂了,我的分全给你.
注意:object pascal认为每个指针是相异的
如
var a:^integer;
b:^integer;//a,b是不能互相替用的
//必须定义一新的变量进行转换
type
Myint=^integer;
var a,b:Myint;//现在,a,b才相容
//例子
procedure TForm1.Button1Click(Sender: TObject);
var pint:^Integer; //整型指针
//pp:pointer; //无类型指针
i:integer;
begin
i:=10;new(Pint);//分配空间
pint^:=i;Showmessage(inttostr(pint^));
dispose(Pint);//释放空间
end;
Procedural types allow you to treat procedures and functions as values that can be assigned to variables or passed to other procedures and functions. For example, suppose you define a function called Calc that takes two integer parameters and returns an integer:function Calc(X,Y: Integer): Integer;You can assign the Calc function to the variable F:var F: function(X,Y: Integer): Integer;F := Calc;If you take any procedure or function heading and remove the identifier after the word procedure or function, what抯 left is the name of a procedural type. You can use such type names directly in variable declarations (as in the example above) or to declare new types:type TIntegerFunction = function: Integer;
TProcedure = procedure;
TStrProc = procedure(const S: string);
TMathFunc = function(X: Double): Double;
var
F: TIntegerFunction; { F is a parameterless function that returns an integer }
Proc: TProcedure; { Proc is a parameterless procedure }
SP: TStrProc; { SP is a procedure that takes a string parameter }
M: TMathFunc; { M is a function that takes a Double (real) parameter and returns a Double }
procedure FuncProc(P: TIntegerFunction); { FuncProc is a procedure whose only parameter
is a parameterless integer-valued function }The variables above are all procedure pointers梩hat is,
pointers to the address of a procedure or function.
If you want to reference a method of an instance object (see Classes and objects),
you need to add the words of object to the procedural type name. For exampletype TMethod = procedure of object;
TNotifyEvent = procedure(Sender: TObject) of object;These types represent method pointers. A method pointer is really a pair of pointers;
the first stores the address of a method, and the second stores a reference to the object
the method belongs to. Given the declarationstype TNotifyEvent = procedure(Sender: TObject) of object;
TMainForm = class(TForm)
procedure ButtonClick(Sender: TObject);
...
end;
var
MainForm: TMainForm;
OnClick: TNotifyEventwe could make the following assignment.OnClick := MainForm.ButtonClick;Two procedural types are compatible if they havethe same calling convention,
the same return value (or no return value), and
the same number of parameters, with identically typed parameters in corresponding positions. (Parameter names do not matter.)Procedure pointer types are always incompatible with method pointer types. The value nil can
be assigned to any procedural type.
Nested procedures and functions (routines declared within other routines) cannot be used
as procedural values, nor can predefined procedures and functions. If you want to use a predefined routine like Length as a procedural value, write a wrapper for it:function FLength(S: string): Integer;begin
Result := Length(S);
end;Procedural types in statements and expressions