Discussion:
[fpc-pascal] Object questions
Ryan Joseph
2018-12-04 02:36:14 UTC
Permalink
Some questions about old-style objects.

1) How can you check if self is assigned? self refers to the struct itself but if the object was allocated on the stack then how do we know self is a pointer or not?

procedure TMyObject.Free;
begin
if self <> nil then
dispose(self,Destroy);
end;

2) Is there any TObject support in objects? I know there is a VMT table for objects but I’m not sure if it’s structure is public in the RTL.

class function TMyObject.ClassName:string;
begin
result := PVmt(self)^.vClassName^;
end;

3) why are operator overloads not supported in objects? Is there a technical reason for that?

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailma
Sven Barth via fpc-pascal
2018-12-04 06:52:09 UTC
Permalink
Post by Ryan Joseph
Some questions about old-style objects.
1) How can you check if self is assigned? self refers to the struct itself
but if the object was allocated on the stack then how do we know self is a
pointer or not?
procedure TMyObject.Free;
begin
if self <> nil then
dispose(self,Destroy);
end;
The type of "Self" inside a object is always the same no matter if it's
allocated on the stack or the heap.
Post by Ryan Joseph
2) Is there any TObject support in objects? I know there is a VMT table
for objects but I’m not sure if it’s structure is public in the RTL.
class function TMyObject.ClassName:string;
begin
result := PVmt(self)^.vClassName^;
end;
Objects don't have TObject-like functionality. There is a VMT record, but
that is only available if there is at least one virtual method.
Post by Ryan Joseph
3) why are operator overloads not supported in objects? Is there a
technical reason for that?
Operator overloads inside records were added much later than support for
global operator overloads and because of Delphi compatibility. Thus there
was simply no need to add support for them.
In addition to that one would need to dereference object pointer variables
to use the operators (though that one is true for records as well).

Regards,
Sven
Ryan Joseph
2018-12-04 07:00:14 UTC
Permalink
The type of "Self" inside a object is always the same no matter if it's allocated on the stack or the heap.
Then there’s now way to do a free method like I showed?
2) Is there any TObject support in objects? I know there is a VMT table for objects but I’m not sure if it’s structure is public in the RTL.
class function TMyObject.ClassName:string;
begin
result := PVmt(self)^.vClassName^;
end;
Objects don't have TObject-like functionality. There is a VMT record, but that is only available if there is at least one virtual method.
I got a crash on that code but maybe it’s because of “self” or is the VMT table for objects not “VMT” from the RTL?

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/
Sven Barth via fpc-pascal
2018-12-04 10:26:39 UTC
Permalink
Post by Sven Barth via fpc-pascal
On Dec 4, 2018, at 1:52 PM, Sven Barth via fpc-pascal <
The type of "Self" inside a object is always the same no matter if it's
allocated on the stack or the heap.
Then there’s now way to do a free method like I showed?
Correct.
Post by Sven Barth via fpc-pascal
2) Is there any TObject support in objects? I know there is a VMT table
for objects but I’m not sure if it’s structure is public in the RTL.
class function TMyObject.ClassName:string;
begin
result := PVmt(self)^.vClassName^;
end;
Objects don't have TObject-like functionality. There is a VMT record,
but that is only available if there is at least one virtual method.
I got a crash on that code but maybe it’s because of “self” or is the VMT
table for objects not “VMT” from the RTL?
The VMT for objects is currently only available locally inside the Objects
unit (search for TVMT there).
There is a bug report however to publish it... Though even then it is much
less powerful than a class - there's a reason Borland reinvented the wheel
there.

Regards,
Sven
Continue reading on narkive:
Loading...