Reputation: 13367
In my program I do:
var aObj: Tobject;
var aObjClassType: Tclass;
....
aObjClassType := aObj.ClassType;
....
aObj.free;
aObj := nil;
....
showmessage(aObjClassType.Classname);
this work but I m not quite sure If this is correct, especially when i read the function TObject.ClassType
function TObject.ClassType: TClass;
begin
Pointer(Result) := PPointer(Self)^;
end;
So does freeing aObj
will not also free the aObjClassType
?
Upvotes: 1
Views: 985
Reputation: 612993
A TClass
is a class. A TObject
is an instance. So obj.ClassType
returns the class, that is the type, of the instance obj
.
Note that this is the runtime type of the instance rather than the type of the obj
reference variable. This is relevant when using polymorphism. So if you write
var
shape: TShape;
....
shape := TSquare.Create;
Then shape.ClassType
returns TSquare
even though the shape
variable is TShape
.
So does freeing
aObj
will not also free theaObjClassType
?
No. Classes are static and created when the module loads and destroyed when the module unloads.
For more detail read the documentation: http://docwiki.embarcadero.com/RADStudio/en/Classes_and_Objects_(Delphi)#TObject_and_TClass
Upvotes: 6