Recent

Author Topic: Casts  (Read 3026 times)

timppl

  • Jr. Member
  • **
  • Posts: 70
Casts
« on: August 03, 2012, 12:19:32 pm »
Hello all

As I understand it, there are two different ways of doing a cast in pascal

1) the 'as' keyword   eg  Result := LCLObject as TWinControl;

2)  using brackets, so the above example is
      Result := TWinControl(LCLObject);


Is there / should there be any difference between these methods?

I ask because when in debug mode , I get an invalid cast error with the first method at lcl/interfaces/gtk2/gtk2cellrenderer.pas line92 , but the second works fine.

using Laz + fpc from svn each day on mandriva linux

Regards

Tim
Mageia 2 Linux on x86

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8492
  • FPC developer.
Re: Casts
« Reply #1 on: August 03, 2012, 01:02:24 pm »
As I understand it, there are two different ways of doing a cast in pascal

1) the 'as' keyword   eg  Result := LCLObject as TWinControl;

Only available for classes (and interfaces). It checks if lclobject is a TWincontrol and throws an exception otherwise.

Quote
2)  using brackets, so the above example is
      Result := TWinControl(LCLObject);

First. This is only a (true) cast if sizeof(result)=sizeof(twincontrol)=sizeof(lclobject), otherwise it is a conversion.
It helps to view (true) casts as "let the compiler reinterpret a piece of memory in a different way described by this type"

Quote
Is there / should there be any difference between these methods?

Yes, this does always assign, and no checking. So it will put lclobject into result even if it is not twincontrol

Quote
I ask because when in debug mode , I get an invalid cast error with the first method at lcl/interfaces/gtk2/gtk2cellrenderer.pas line92 , but the second works fine.

This is the reason to use AS. If something is wrong, you get a warning.  Casting, while removing the warning will pass on corrupt data.

Solution: fix the original problem, and make sure it IS truly a twincontrol
 

timppl

  • Jr. Member
  • **
  • Posts: 70
Re: Casts
« Reply #2 on: August 06, 2012, 08:52:52 am »
Thanks marcov, very clear explanation. I will investigate, I think it is something in the LCL

Regards

Tim
Mageia 2 Linux on x86

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8492
  • FPC developer.
Re: Casts
« Reply #3 on: August 06, 2012, 09:32:26 am »

Code: [Select]
if assigned(lclobject) then
  begin
    if not (lclobject is TWincontrol) then
        showmessage('lclobject not ok : '+lclobject.classname);
    end
 else
 showmessage('lclobject not assigned');

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Casts
« Reply #4 on: August 06, 2012, 01:56:31 pm »
TObject derivative objects all have same sizeof() do they not?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8226
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Casts
« Reply #5 on: August 06, 2012, 02:14:53 pm »
Quote
TObject derivative objects all have same sizeof() do they not?
Yes, they're all = SizeOf(Pointer).

 

TinyPortal © 2005-2018