Recent

Author Topic: Comparing pointers to base objects  (Read 497 times)

paulvanderlinden

  • New member
  • *
  • Posts: 6
Comparing pointers to base objects
« on: July 18, 2022, 01:50:37 pm »
Hi,

First off, not sure if this is the correct subforum, please inform me if not.
In delphi, this was perfectly valid code:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4. Pbbase = ^Obbase;
  5. Obbase = object
  6. end;
  7.  
  8. Pbderived = ^Obderived;
  9. Obderived = object(Obbase)
  10. end;
  11.  
  12. var base:PbBase;
  13.       Derived:Pbderived;
  14. begin
  15.   new(Base);
  16.   new(Derived);
  17.   if Base=Derived then
  18.     writeln('yes');
  19. end.

However compiling in lazarus/freepascal gives me an error about incompatible types Pbbase and Pbderived.
I know I could put a cast around it (
Code: Pascal  [Select][+][-]
  1. if Base=PbBase(Derived) then
but that is a bit to non-restrictive, if f.e. I decouple base from derived the cast would still compile....

Paul

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10291
  • FPC developer.
Re: Comparing pointers to base objects
« Reply #1 on: July 18, 2022, 02:00:58 pm »
It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!

You can try to file a bug, but as it is very legacy functionality, it might lay a while.

paulvanderlinden

  • New member
  • *
  • Posts: 6
Re: Comparing pointers to base objects
« Reply #2 on: July 18, 2022, 02:18:36 pm »
Ok, thanks.
So definitely not a setting I missed.

Turned out, assigning the derived to a tempvar of type base does the trick too.
Not the cleanest solution, but at least it's the same functionality. and with a bit of luck, this will be optimized out by the compiler anyway

ASerge

  • Hero Member
  • *****
  • Posts: 1985
Re: Comparing pointers to base objects
« Reply #3 on: July 20, 2022, 09:59:38 pm »
It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!
From Turbo Pascal 5.5 docs:
Quote
Type compatibility also operates between pointers to object types, under the same general rules as with instances of object types: Pointers to descendants can be assigned to pointers to ancestors.
The Delphi compiler still supports this.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10291
  • FPC developer.
Re: Comparing pointers to base objects
« Reply #4 on: July 21, 2022, 09:19:30 am »
It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!
From Turbo Pascal 5.5 docs:
Quote
Type compatibility also operates between pointers to object types, under the same general rules as with instances of object types: Pointers to descendants can be assigned to pointers to ancestors.
The Delphi compiler still supports this.

This is not an assignment, but a comparison. But it would be nice to know what TP/BP 7 (our reference version) does with this code.

Thaddy

  • Hero Member
  • *****
  • Posts: 11926
Re: Comparing pointers to base objects
« Reply #5 on: July 24, 2022, 04:52:54 pm »
@Marco
TP does a deep comparison, so a full run over both objects. as opposed to a shallow comparison, where just the pointers and size need to be equal.
Note that deep comparisons are rather slow, especially in a loop. Delphi does still a deep comparison on objects. FPC doesn't.
So it is a - afaik not documented! - compatibility issue. Either that, or if this was not intentional it is a bug.
« Last Edit: July 24, 2022, 07:43:11 pm by Thaddy »
Black themes should be banned.

 

TinyPortal © 2005-2018