Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a type safe version of the @ operator? Or a compiler warning that can be switched on?

Tags:

delphi

The following code does not generate a compiler warning in D6. Can I make it warn me about pointing ps at an integer when I have told it that ps points to a string?

procedure Test;
var
  i: integer;
  s, m: string;
  ps: ^string;
begin
  s := 'Test message';
  ps := @s;
  m := ps^;
  MessageDlg(m, mtInformation, [mbOK], 0);  // This displays 'Test message'.
  ps := @i;  // I would like a warning here.
  m := ps^;
  MessageDlg(m, mtInformation, [mbOK], 0);  // This might display garbage.
end;
like image 646
soid Avatar asked Jan 22 '23 13:01

soid


2 Answers

Use Type-checked pointers ({$T+} directive)

like image 105
kludg Avatar answered Feb 01 '23 23:02

kludg


There's an option in Project settings -> Compiler settings exactly for this.

like image 45
Eugene Mayevski 'Callback Avatar answered Feb 01 '23 22:02

Eugene Mayevski 'Callback