Interfaces

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Interfaces

 

In Delphi interface types can be defined like in the following lines of code:

 

 

IConverter = interface

  ['{GUID}']

  function convert(Source : String): String;

end;

 

TConverter = class(TInterfacedObject, IConverter)

public

  //...

  function convert(Source : String): String;

end;  

 

 

For C++Builder the special macro "__interface" macro:

 

#define __interface struct  // in sysmac.h

 

is used instead of the class keyword  to mark interfaces:

 

 

 

__interface INTERFACE_UUID("{ GUID}" ) IConverter

{

  virtual String __fastcall convert(String Source);

};

 

class TConverter : public TInterfacedObject, public IConverter

{

  //...

  String __fastcall convert(String Source);

};

 

 

However there will be a linker error like "unresolved vtable", if such an interface is used. If you create a small pas file for the interface, add it to the C++Builder project and remove the C++ definition for the interface, C++Builder will create a header file for the interface file automatically, which you can include then. Example pas file:

 

unit Recyclable;

 

interface

 

 type

    IRecyclable = Interface(IInterface)

      function GetIsRecyclable : Boolean;

      property isRecyclable : Boolean read GetIsRecyclable;

    end;

 

implementation

end.

 

 

->

 

#include "Recyclable.hpp"

 

Now the project will compile without linker error.

 

 

Visual C++ also knows this keyword, but the GUID has to be written differently:

 

 

[ uuid( "GUID" ) ]

__interface IConverter

{

  virtual String convert(String Source);

};

 

class TConverter : public TInterfacedObject, IConverter

{

  //...

  String convert(String Source);

};

 

 

At other compilers, which have not the interface extension, multiple inheritance can be used instead, As explained here:

 

http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C

 

 

the interface class needs a virtual destructor and the methods should be public and declared abstract:

 

 

//[ uuid( "GUID" ) ]

class IConverter

{

  public:

  virtual ~IConverter() {}

  virtual String convert(String Source) = 0;

};

 

class TConverter : public TInterfacedObject, IConverter

{

  //...

  String convert(String Source);

};

 

 

GUID's cannot be used here. Under Microsoft Windows GUID's are used for COM purposes.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content