Forum > General

Comparing pointers to base objects

(1/2) > >>

paulvanderlinden:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; typePbbase = ^Obbase;Obbase = objectend; Pbderived = ^Obderived;Obderived = object(Obbase)end; var base:PbBase;      Derived:Pbderived;begin  new(Base);  new(Derived);  if Base=Derived then    writeln('yes');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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
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:

--- Quote from: marcov on July 18, 2022, 02:00:58 pm ---It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!

--- End quote ---
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.
--- End quote ---
The Delphi compiler still supports this.

marcov:

--- Quote from: ASerge on July 20, 2022, 09:59:38 pm ---
--- Quote from: marcov on July 18, 2022, 02:00:58 pm ---It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!

--- End quote ---
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.
--- End quote ---
The Delphi compiler still supports this.

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version