Virtual functions at construction

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > Constructors > Problems with constructors > Virtual functions at construction

 

Another problem with constructors is that calls of virtual functions inside of constructors are allowed in Delphi, but in C++ such calls should be avoided

 

As example the TPolygon class and the derived classes TTriangle and TSquare as defined here

 

http://www.delphibasics.co.uk/RTL.asp?Name=Abstract

 

can be taken:

 

  TPolygon = class

  ...

  protected

     procedure setArea; Virtual; Abstract;  // Cannot code until sides known

  ...

 

  TTriangle = class(TPolygon)

  protected

    procedure setArea; override;   // Override the abstract method

 

 

  TSquare = class(TPolygon)

  protected

    procedure setArea; override;   // Override the abstract method

 

 

constructor TPolygon.Create(sides, length : Integer);

begin

  ...

  setArea;

end;

 

 

In Delphi the setAres-procedure of the derived classes will be called in their constructors. With C++Builder this works well too, but other C++ compilers always try to call the setArea-procedure of TPolygon. Manual post-processing is necessary then. E.g. setArea could be made accessible and called after construction:

 

triangle = new TTriangle(3, 10);

triangle->setArea();

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content