Delphi Self-Pointer usage -
i need pointer class instance inside instance. can't use "self" directly, need store pointer future usage. tried next code:
type ttest = class(tobject) public class function getclasspointer: pointer; function getselfpointer: pointer; end; class function ttest.getclasspointer: pointer; begin result := pointer(self); end; function ttest.getselfpointer: pointer; begin result := pointer(self); end;
and both result wrong - code:
test := ttest.create; writeln('actual object address: ', inttohex(integer(@test), 8)); writeln('class "self" value: ', inttohex(integer(test.getclasspointer()), 8)); writeln('object "self" value: ', inttohex(integer(test.getselfpointer()), 8));
returns:
actual object address: 00416e6c class "self" value: 0040e55c object "self" value: 01ee0d10
please, me understand, "self" value ? "self" pointer class instance ? how use pointer future use outside of object ? how proper pointer value ?
you're trying compare 3 different entities.
@test returns address of variable test, not object instance points to.
test.getclasspointer() returns address of class metadata, constant data structure generated compiler runtime can find virtual method table, runtime type info tables, , more. instances of class share same class metadata structure. pointer class metadata object instance's type identity - it's how object knows type @ runtime.
test.getselfpointer() gives actual address of object instance in memory. 2 object instances (created separately) have different instance addresses. test.getselfpointer() equal contents of test instance variable: pointer(test)
for example (pseudocode, not tested):
type ttest = class end; var test1: ttest; test2: ttest; begin test1 = ttest.create; // allocates memory global heap, stores pointer test2 = test1; // copies pointer object test2 variable writeln("test1 variable points to: ", inttohex(integer(pointer(test1)))); writeln("test2 variable points to: ", inttohex(integer(pointer(test1)))); end.
Comments
Post a Comment