PDA

View Full Version : typcasting



tux
16-04-2005, 04:55 PM
ive got a class called TRGLPhysicsObject (class of TObject) and a pointer to that class, whats faster (or whats best)...



var
LPhysicsObject: TRGLPhysicsObject;
LPointer: Pointer;

Begin
LPhysicsObject := LPointer;

LPhysicsObject.bla := 123;


or



var
LPointer: Pointer;

Begin
TRGLPhysicsObject(LPointer).bla := 123;


just wondering because a callback went horribly wrong then i changed a few things and now it works fine

M109uk
16-04-2005, 05:59 PM
When i use pointers i find it easier to type cast it as:

type
PRGLPhysicsObject = ^TRGLPhysicsObject;
TRGLPhysicsObject = Class(TObject)
// code
end;

////

var
LPhysicsObject: PRGLPhysicsObject;
begin
LPhysicsObject.bla := 123;

This way you can use all the functions and propertise in that type..

tux
16-04-2005, 06:27 PM
but you would still have to assign it wouldnt you, or is it faster to assign it to a PRGLPhysicsObject instead of a TRGLPhysicsObject?

{MSX}
16-04-2005, 06:37 PM
Tux, the cast takes no time in compiled code, it is resolved by the compiler.
The assignement instead would take an extra instruction but i'm confident that the optimizer can figure it out that it's unnecessary and can be removed.
So at the end it shouldn't be different.

Also, i always suggest to avoid thinking too much on almost this type of micro-optimization when maybe in other part of the program there's some nested loops or such that loses hundred of times more than the assignement.

BTW i never see the use of a pointer to a class, as in
M109uk code.. Classes are already pointers.

tux
16-04-2005, 07:06 PM
thanks, i keep forgetting the compiler knows best :)

in that case im not sure what i did to stop the code falling to 2fps :/