i'm looking to understand

  • virtual
  • override
  • overload
  • reintroduce

when applied to object constructors. Every time i randomly add keywords until the compiler shuts up - and (after 12 years of developing with Delphi) i'd rather know what i'm doing, rather than trying things randomly.

Given a hypothetical set of objects:

TComputer = class(TObject)
public
    constructor Create(Cup: Integer); virtual;
end;

TCellPhone = class(TComputer)
public
    constructor Create(Cup: Integer; Teapot: string); virtual;
end;

TiPhone = class(TCellPhone)
public
    constructor Create(Cup: Integer); override;
    constructor Create(Cup: Integer; Teapot: string); override;
end;

The way i want them to behave is probably obvious from the declarations, but:

  • TComputer has the simple constructor, and descendants can override it
  • TCellPhone has an alternate constructor, and descendants can override it
  • TiPhone overrides both constructors, calling the inherited version of each

Now that code doesn't compile. i want to understand why it doesn't work. i also want to understand the proper way to override constructors. Or perhaps you could never override constructors? Or perhaps it is perfectly acceptable to override constructors? Perhaps you should never have multiple constructors, perhaps it is perfectly acceptable to have multiple constructors.

i want to understand the why. Fixing it would then be obvious.

See also

Edit
Report