Recent

Author Topic: Question about TInterfacedObject  (Read 4280 times)

ricardo_sdl

  • New Member
  • *
  • Posts: 21
Question about TInterfacedObject
« on: November 18, 2015, 06:17:12 pm »
Why do I need to implement TInterfacedObject when implementing my own interfaces? Example:
I created the following interface:
Code: Pascal  [Select][+][-]
  1. unit updatable;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.   IUpdatable = interface
  12.     procedure Update;
  13.   end;
  14.  
  15. implementation
  16.  
  17. end.
  18.  

Then I created the following class:
Code: Pascal  [Select][+][-]
  1. unit gameobject;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, updatable;
  9.  
  10. type
  11.  
  12.   { TGameObject }
  13.  
  14.   TGameObject = class(TInterfacedObject, IUpdatable)
  15.     procedure Update;
  16.     procedure Draw;
  17.  
  18.  
  19.   end;
  20.  
  21. implementation
  22.  
  23. { TGameObject }
  24.  
  25. procedure TGameObject.Update;
  26. begin
  27.  
  28. end;
  29.  
  30. procedure TGameObject.Draw;
  31. begin
  32.  
  33. end;
  34.  
  35. end.
  36.  
  37.  

Why if I remove the TInterfacedObject from the declaration of TGameObject I get the following errors?
Compile Project, Target: TestGame.exe: Exit code 1, Errors: 6
gameobject.pas(14,17) Error: No matching implementation for interface method "IUnknown.QueryInterface(constref TGuid,out <Formal type>):LongInt; StdCall;" found
gameobject.pas(14,17) Error: No matching implementation for interface method "IUnknown._AddRef:LongInt; StdCall;" found
gameobject.pas(14,17) Error: No matching implementation for interface method "IUnknown._Release:LongInt; StdCall;" found
gameobject.pas(14,17) Error: No matching implementation for interface method "IUnknown.QueryInterface(constref TGuid,out <Formal type>):LongInt; StdCall;" found
gameobject.pas(14,17) Error: No matching implementation for interface method "IUnknown._AddRef:LongInt; StdCall;" found
gameobject.pas(14,17) Error: No matching implementation for interface method "IUnknown._Release:LongInt; StdCall;" found

Thanks!


Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Question about TInterfacedObject
« Reply #1 on: November 18, 2015, 06:57:56 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   IUpdatable = interface
  3.     procedure Update;
  4.   end;
  5.  
First of all, FPC defaults to COM style interfaces, thus the above code defaults to:

Code: Pascal  [Select][+][-]
  1.   IUpdateable = interface(IUnknown)
  2.    ...
  3.   end
  4.  

If you don't want to use COM Interfaces, and you don't want memory managed interfaces (where memory is freed automatically for you), and you don't want to use TInterfacedObject (which implement's COM's IUnknown), then you need to tell FPC that. More specifically, you need to tell it you want to use CORBA style interfaces instead. As follows:

Code: Pascal  [Select][+][-]
  1. unit updatable;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$interfaces corba}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils;
  10.   IUpdateable = interface   // this is not an IUnknown interface now
  11.    ...
  12.   end
  13.  

Thus your interface implementation can now look as follows:

Code: Pascal  [Select][+][-]
  1. unit gameobject;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$interfaces corba}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, updatable;
  10.  
  11. type
  12.  
  13.   { TGameObject }
  14.  
  15.   TGameObject = class(TObject, IUpdatable) // Don't need to use TInterfacedObject any more
  16.   private
  17.     procedure Update;
  18.    ...
  19.   end;
  20. ...
  21.  


ps:
The default COM style interface used by FPC is a cross-platform Interface style. The name COM is just a legacy name. And under Windows those COM style interfaces can obviously be used with Windows COM too. Often developers like to stick to the COM style interfaces because of the automatic memory management - but that is not always ideal in all situations. So FPC gives you the choice.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

ricardo_sdl

  • New Member
  • *
  • Posts: 21
Re: Question about TInterfacedObject
« Reply #2 on: November 19, 2015, 12:10:51 am »
Thanks for the explanation, and your corba example worked perfectly.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Question about TInterfacedObject
« Reply #3 on: November 19, 2015, 10:59:10 am »
Excellent - glad I could help.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

 

TinyPortal © 2005-2018