DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Pascal and Delphi (http://www.daniweb.com/forums/forum124.html)
-   -   using method or dereferenced object pointer (http://www.daniweb.com/forums/thread123796.html)

toxygen May 12th, 2008 4:22 am
using method or dereferenced object pointer
 
I would like to ask whether pointer dereference by typecast is same in both cases:
TComponent(my_tfplist[1]).mymethod
and
 TComponent(my_tfplist[1]^).mymethod

both things are doing the same thing (at least it behaves so), but compiler generates different code, so i'm curious what is the difference between those two.

to clarify:
my_tfplist is an object instance of class tfplist (or tlist, doesn't matter really).

thanks

Duoas May 12th, 2008 1:28 pm
Re: using method or dereferenced object pointer
 
In Delphi (as in C++, etc.), a reference is really a veiled pointer. Hence, when you declare a variable of type
var TMyObject: MyObject;
space is not reserved for the entire object; rather, MyObject is a pointer to a TMyObject.

The compiler is smart enough to know the difference, but allows for the possibility that the programmer knows something it doesn't when you use the second syntax you illustrated. However, unless you really do know something funny about your variable, you should stick to the first syntax.

Hope this helps.

toxygen May 12th, 2008 1:55 pm
Re: using method or dereferenced object pointer
 
Quote:

Originally Posted by Duoas (Post 605547)
However, unless you really do know something funny about your variable, you should stick to the first syntax.

Hope this helps.



thank you very much.


All times are GMT -4. The time now is 4:47 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC