请教一个指针的问题 ( 积分: 20 )

  • 主题发起人 主题发起人 awfigsk
  • 开始时间 开始时间
A

awfigsk

Unregistered / Unconfirmed
GUEST, unregistred user!
type
TDerived=class
public
function mymethod2;boolean;
end;
var
p:pointer;
begin
[red]p:=pointer(@TDerived.mymethod2)

与 p:=@TDerived.mymethod2有何区别?[/red]
 
type
TDerived=class
public
function mymethod2;boolean;
end;
var
p:pointer;
begin
[red]p:=pointer(@TDerived.mymethod2)

与 p:=@TDerived.mymethod2有何区别?[/red]
 
感觉好像没区别把?@就是取地址,加上pointer只是告诉编译器强制转换成pointer类型,不过,如果不用pointer,编译器也应该知道@取得的是pointer啊。呵呵,请高手来解释把。我随便说说而已。
 
2个没区别的。。

就像
var
str:pchar;
begin
str:='aaaaaaaaa';
str:=pchar('aaaaaaaa');
end;
 
呵呵,@取得的不是简简单单的Pointer阿

The @ operator returns the address of a variable, or of a function, procedure, or method
that is, @ constructs a pointer to its operand. For more information about pointers, see Pointers and pointer types . The following rules apply to @.

If X is a variable, @X returns the address of X. (Special rules apply when X is a procedural variable
see Procedural types in statements and expressions .) The type of @X is Pointer if the default {$T} compiler directive is in effect. In the {$T+} state, @X is of type ^T, where T is the type of X (this distinction is important for assignment compatibility, see Assignment-compatibility).
If F is a routine (a function or procedure), @F returns F's entry point. The type of @F is always Pointer.
When @ is applied to a method defined in a class, the method identifier must be qualified with the class name. For example,
@TMyClass.DoSomething
points to the DoSomething method of TMyClass. For more information about classes and methods, see Classes and objects.
Note:
When using the @ operator, it is not possible to take the address of an interface method as the address is not known at compile time and cannot be extracted at runtime.
 
p和mymethod2是两个不同的指针,但指向了同一块地方
 
效果一样的
 
自己检查下不就知道有没有区别了吗?
 
我就是运行程序后,发现结果都是一样的。
 
看到指针就头大!
 
晕。。。。Object Pascal里基本都是指针啊。。。。。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
694
import
I
后退
顶部