Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-12

Object questions

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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;

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

Reply by email

Sven Barth
to Ryan Joseph

Am Di., 4. Dez. 2018, 04:06 hat Ryan Joseph
geschrieben:

Show quoted text 12 lines
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.

Show quoted text 8 lines
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.

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

Reply by email

Ryan Joseph
to Sven Barth
On Dec 4, 2018, at 1:52 PM, Sven Barth via fpc-pascal wrote: 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?

Show quoted text 9 lines
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

Reply by email

Sven Barth
to Ryan Joseph

Am Di., 4. Dez. 2018, 08:00 hat Ryan Joseph
geschrieben:

Show quoted text 10 lines
> On Dec 4, 2018, at 1:52 PM, Sven Barth via fpc-pascal < > wrote: > > 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.

Show quoted text 15 lines
> > 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

Reply by email