delphi overload, override, virtual method

13,808

Without putting focus on your bad constructor implementation,

your problem is that both the ancestor and the child class are defined in the same unit, therefor the standard definition of Private/Protected does not apply here.

If you want to prevent the ancestor constructor (which you are overriding in the child class) from showing up as a code parameter when instantiating an object of that derrived class then simply make it a member of the strict protected or strict private section.

With your example :

TLiveThing=class
strict protected
 constructor Create(whereLive:string); virtual;
end;

THuman=class(TLiveThing)
public
 constructor Create(whereLive:string); overload; override;
 constructor Create(age:integer); overload;
end;

This will prevent the ancestor constructor Create(whereLive:string) from showing up as a parameter when you are creating an instance of your child class.

As pointed out by David, this has nothing to do with hiding the default Create constructor, it's only viable for hiding your custom constructors.

Share:
13,808
navirius
Author by

navirius

Updated on June 04, 2022

Comments

  • navirius
    navirius almost 2 years

    have simple object hierarchy like below

    TLiveThing=class
    protected
     FTest:string;
     constructor Create(whereLive:string);overload;virtual;
     constructor Create(haveBone:Boolean);overload;virtual;
    end;
    
    THuman=class(TLiveThing)
    public
     constructor Create(whereLive:string);overload;override;
     constructor Create(age:integer);overload;
    end;
    

    in theoretically, if I instantiate THuman, I have 2 constructor, but in fact I have 5 constructor displayed by code insight, actually I want to see 3 constructor, - Create(whereLive:String), overriden - Create(age:integer) - Create(haveBone:integer)

    human:=THuman.Create(       <=====in there I have 5 suggestion constructor
    

    why I have this strange behaviour? how to fix it, because it so anoying, I cant always check my class that I need to instantiate, and if I instantiate like below

    human:=THuman.Create(); <===== it doesnt give me error
    

    how I completely hide my anchestor constructor? , because if I instatiate like above, completely give me a wrong object

    UPDATE: and also I can see default Create without parameter from TObject too

  • David Heffernan
    David Heffernan almost 11 years
    That won't help at all. The parameterless TObject.Create is made visible by the introduction of an overloaded Create. See the duplicate.
  • Peter
    Peter almost 11 years
    Yes, but also he said that he wants to hide the ancestor constructor Create(whereLive:string) and this will at least help him hide that.
  • navirius
    navirius almost 11 years
    so, thats mean, in delphi, if I have 2 virtual constructor on base class, then derived class have 1 constructor and 1 overriden from base, then when I instantiate object from derived class, I can have 4 constructor, include the overriden one ( I got that experience from my code at my question) thats bug or just like that? (because I dont get any behavior like that on c#)
  • navirius
    navirius almost 11 years
    because, actually I dont want hide some constructor with strict private, I just want, that constructor is overriden by derived class, so if I instantiate object, I can see overriden constructor just constructor from derived object, my experience in delphi, code insight give me suggestion constructor from base class and constructor from derived class, though I already override that constructor
  • Peter
    Peter almost 11 years
    @navirius , the strict word has no influence on the override directive of a method, the result will be the same. I think you got confused by the word hidden in my answer. The ancestor virtual constructor which you decided to override in your derived class will still be overriden, the word strict just means that if both Parent and Child class are defined in the same unit, it will ommit the confusion that you are now experiencing when instantiating an object. You could achieve the same result by writing Ancestor class in Unit1 and Child class in Unit2. I will update answer, hope you accept it.