Forum > General

Accessing Property via Pointer

(1/2) > >>

kupferstecher:
Hello all,

see below code, I try to set the value of a property through a pointer. The property is mapped to a function, so I wished the compiler would call the setter, by dereferencing the pointer pointing to the property. I guessed that it isn't even possible so I made a little test (below code). And I didn't get it to work, the function isn't called. But also no warning/error is given by the compiler. Is this by design?

So what actually happens in line 27?

Thanks!

(FPC3.2.2, 64 bit)

--- 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; Type TFoo= class  fSth:Integer;  procedure setSth(AValue:Integer);  Property sth: Integer read fSth write setSth;end; procedure TFoo.setSth(AValue:Integer);begin  fSth:= AValue;  writeln('fSth:',fSth);end; var  Foo: TFoo;  pSth: ^Integer; begin   Foo:= TFoo.create;  Foo.sth:= 3;           //-> output 'fSth:3'   pSth:= @Foo.sth;  pSth^:= 7;             //-> No output, expected 'fSth: 7'   readln;end.  

Fibonacci:
It just doesnt work like that. You directly accessed the memory and changed the value to 7, dont expect for the setter to be called.

Thaddy:
It is only possible through assembler.

kupferstecher:

--- Quote from: Fibonacci on February 20, 2024, 01:41:28 pm ---You directly accessed the memory and changed the value to 7, dont expect for the setter to be called.

--- End quote ---
But which memory... After a further test it seems when setting the pointer with @Foo.Sth the "read" part of the property is taken, and when I change the property's read to a method, then there is an error. It's understandable for me now, but a pity, that it won't work.

Martin_fr:
Afaik, you shouldn't even be able to take the address of a property

--- 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";}};} --- pSth:= @Foo.sth;That should generate an error.

In the same way as

--- 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";}};} --- inc(Foo.sth);gives an error.


There is an existing report https://gitlab.com/freepascal.org/fpc/source/-/issues/30017

From other similar prior cases I personally would expect that the fix will be to make both properties give an error when their address is taken.

Navigation

[0] Message Index

[#] Next page

Go to full version