Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Unterschied zwischen (var AFileStream: TFileStream) und (AFileStream: TFileStream

  Alt 12. Mai 2014, 17:41
Es gibt call by value, call by reference und const parameter.

Ein kleiner Test zeigt die Unterschiede
Delphi-Quellcode:
program dp_180328;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

type
  TTest = class
  end;

procedure Output( const AContext : string; AInstance : TTest );
begin
  if Assigned( AInstance )
  then
    Writeln( AContext, ': ', Integer( AInstance ) )
  else
    Writeln( AContext, ': NIL' )

end;

procedure CallByValue( AInstance : TTest );
begin
  AInstance := TTest.Create;
  Output( 'CallByValue', AInstance );
end;

procedure CallByReference( var AInstance : TTest );
begin
  AInstance := TTest.Create;
  Output( 'CallByReference', AInstance );
end;

procedure Call( const AInstance : TTest );
begin
  // Auskommentiert, da es hier einen Compiler-Fehler gibt!
  // AInstance := TTest.Create;
  // Output( 'Call', AInstance );
end;

procedure Main;
var
  LTest : TTest;
begin
  LTest := TTest.Create;
  try
    Output( 'Main', LTest );
    CallByValue( LTest );
    Output( 'Main', LTest );
    CallByReference( LTest );
    Output( 'Main', LTest );
    Call( LTest );
    Output( 'Main', LTest );
  finally
    LTest.Free;
  end;
end;

begin
  try
    Main;
  except
    on E : Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;
  ReadLn;

end.
Die Ausführung ergibt dann
Code:
Main           : 31925952
CallByValue    : 31925968
Main           : 31925952
CallByReference: 31925984
Main           : 31925984
Main           : 31925984
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat