title: Delphi XE7 smart pointers
body: <p>I am new to Delphi with a C++ background and trying to figure out how smart pointers can be implemented. I came across the following post, which I am trying to use as my own starting point: <a href="https://stackoverflow.com/questions/28968208/delphi-smart-pointers-and-generics-tlist">Delphi - smart pointers and generics TList</a></p>
  
  <p>However I can't compile the previous code using Delphi XE7 (compiler errors are shown as comments in code). Also I would greatly appreciate it if someone actually explained the code's logic (initially I wanted to use the class as a drop in utility class but now I would like to understand what is actually happening). I vaguely understand that because the smart pointer implementation is inheriting from TInterfacedObject, it is reference counted but anything beyond that makes no sense to me :)</p>
  
  <pre><code>unit SmartPointer;
  
  interface
  
  uses
    SysUtils, System.Generics.Collections;
  
  type
    ISmartPointer&lt;T&gt; = reference to function: T;
  
    // complains ISmartPointer&lt;T&gt; expecting an interface type
    TSmartPointer&lt;T: class, constructor&gt; = class(TInterfacedObject,ISmartPointer&lt;T&gt;)
    private
      FValue: T;
    public
      constructor Create; overload;
      constructor Create(AValue: T); overload;
      destructor Destroy; override;
      function Invoke: T;
    end;
  
  implementation
  
  { TSmartPointer&lt;T&gt; }
  
  constructor TSmartPointer&lt;T&gt;.Create;
  begin
    inherited;
    FValue := T.Create;
  end;
  
  // complains: overload procedure TSmartPointer.Create must be marked with the overload directive
  constructor TSmartPointer&lt;T&gt;.Create(AValue: T);
  begin
    inherited Create;
    if AValue = nil then
      FValue := T.Create
    else
      FValue := AValue;
  end;
  
  destructor TSmartPointer&lt;T&gt;.Destroy;
  begin
    FValue.Free;
    inherited;
  end;
  
  function TSmartPointer&lt;T&gt;.Invoke: T;
  begin
    Result := FValue;
  end;
  
  end.
  </code></pre>
  
  <p>Tried to use the previous smart pointer with following test code which resulted in a compiler error…what am I missing?</p>
  
  <pre><code>program TestSmartPointer;
  
  {$APPTYPE CONSOLE}
  
  {$R *.res}
  
  uses
    System.SysUtils, SmartPointer;
  
  type
  TPerson = class
    private
      _name : string;
      _age : integer;
    public
  
      property Name: string read _name write _name;
      property Age: integer read _age write _age;
    end;
  
  var
    pperson : TSmartPointer&lt;TPerson&gt;;
  
  begin
    try
      { TODO -oUser -cConsole Main : Insert code here }
      pperson := TSmartPointer&lt;TPerson&gt;.Create();
      // error on next line: undeclared Identifier: Name
      pperson.Name := 'John Doe';
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end.
  </code></pre>