Delphi: Overridden virtual constructor descendant not being called by overload -


yet in series of questions regarding constructors in delphi.

i have base class has has virtual constructor:

tcomputer = class(tobject) public     constructor create(teapot: integer); virtual; end; 

the constructor virtual times needs call

var    computerclass: class of tcomputer;    computer: tcomputer; begin         computer := computerclass.create(nteapot); 

the constructor overridden in descendants:

tcellphone = class(tcomputer)  public    constructor create(teapot: integer); override; end;  tiphone = class(tcellphone )  public    constructor create(teapot: integer); override; end; 

where tcellphone , tiphone descendants each have opportunity own initialization (of members not included readability).

but add overloaded constructor ancestor:

tcellphone = class(tcomputer)  public    constructor create(teapot: integer); override; overload;    constructor create(teapot: integer; handle: string); overload; end; 

the alternate constructor in tcellphone calls other virtual constructor, gets proper overridden behaviour:

constructor tcellphone.create(teapot: integer; handle: string); begin    tcellphone.create(teapot); //call sibling virtual constructor     fhandle := handle; end; 

the problem descendant, overridden, constructor never called. actual stack trace chain of calls is:

phone := tiphone.create(37, 'spout')    constructor tcellphone.create(teapot: integer; handle: string)       constructor tcellphone.create(teapot: integer)          constructor tcomputer.create(teapot: integer)             tobject.create 

the sibling call tcellphone.create(int), virtual, should have called descendant, overridden, method in tiphone:

phone := tiphone.create(37, 'spout')    constructor tcellphone.create(teapot: integer; handle: string)       constructor tiphone.create(teapot: integer)          constructor tcellphone.create(teapot: integer)             constructor tcomputer.create(teapot: integer)                tobject.create 

so seems attempts use sibling virtual constructor delphi not work expected.

is bad idea 1 constructor use another? design intention code in overloaded constructors copy-paste versions of each other?

i notice in .net constructors chain each other:

public bitmap(int width, int height) : this(width, height, pixelformat.format32bppargb) {}  public bitmap(int width, int height, pixelformat format) {...} 

this seems problem if:

  • a constructor virtual
  • you overload constructors

is rule cannot have 1 constructor overload another?

errr..

constructor tcellphone.create(teapot: integer; handle: string); begin    tcellphone.create(teapot); //call sibling virtual constructor     fhandle := handle; end; 

that should be:

constructor tcellphone.create(teapot: integer; handle: string); begin    create(teapot); //call sibling virtual constructor     fhandle := handle; end; 

you creating new tcellphone instance , not calling other create method.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -