Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to put this as comment to Rob Kennedy's answer, but since I can't, here I go..</p> <blockquote> <p>All the while there is no warning about hiding ancestor constructors.</p> </blockquote> <p>Simply because you don't.</p> <blockquote> <p>If i'm hiding the ancestor, why is there no warning? i have no reintroduce.</p> </blockquote> <p>Again, simply because you don't hide anything.</p> <p>You saw the proof that you did not hide anything. It's the 3 available constructors that you've inspected is the proof.</p> <blockquote> <p>Why can reintroduce be used to hide the ancestor?</p> </blockquote> <p>Like Rob mentioned, reintroduce is merely suppressing the compiler hint/warning. There is no real technicality behind that word. Thus you don't hide anything with reintroduce.</p> <p>I would like to put my thought on how to hiding, but I agree with Sertac, first I have to know what is your definition of hiding in this case.</p> <p>Edit: I just read the posts you mentioned, I think you have misunderstood the concepts. Here my short explanation.</p> <blockquote> <p><a href="https://stackoverflow.com/questions/3874330/delphi-how-to-hide-ancestor-constructors">reintroduce is used to hide ancestor constructors</a></p> </blockquote> <p>The answer of that post does not indicate that. The one that really hiding ancestor's constructor is the NEW CONSTRUCTOR of the descendant with the same parameter with the ancestor's. The keyword reintroduce is simply suppressing compiler warning.</p> <blockquote> <p><a href="https://stackoverflow.com/questions/3876484/delphi-how-to-add-a-different-constructor-to-a-descendant">reintroduce is used to show ancestor constructors</a></p> </blockquote> <p>In the answer of that post, it's the OVERLOAD keyword that makes ancestor's constructor still available.</p> <p>ADDITION in response to Ian's question in his comment below:</p> <p>The first step to solve your confusion is to identify the real problems. If we carefully examine you posts, it will become obvious that you actually wanted to solve two problems in one step. The two problems are:</p> <ol> <li>To hide ancestor constructor with specific name</li> <li>To have multiple constructor with same specific name in the descendant.</li> </ol> <p>Although they may seem simple problems, but careful inspection will immediately struck your head that their natures are exactly opposite to each other. Problem 1 wants to hide a method/constructor while problem 2 wants to show not only one but multiple method/constructor. So if you mix them together <strong><em>in one step</em></strong>, they will definitely cancel each others out. No wonder they give you headache... :)</p> <p>The basic rule to solve these two problems is not to mix them in one step. This means we need an intermediate class to solve problem 1, and do the overloading in descendants of that intermediate class. Something like this:</p> <pre><code>type TComputer = class(TObject) private FCaller: TConstructorCaller; public constructor Create(Teapot: string=''); virtual; property Caller: TConstructorCaller read FCaller; end; TBaseCellphone=class(TComputer) constructor Create(Cup: Integer); virtual; end; TCellPhone = class(TBaseCellphone) protected public constructor Create(Cup: Integer); overload; override; constructor Create(Cup: Integer; Teapot: string); overload; virtual; end; TiPhone = class(TCellPhone) public constructor Create(Cup: Integer); reintroduce; overload; constructor Create(Handle: String); reintroduce; overload; end; </code></pre> <p>From the above code, TBaseCellphone is the intermediary class. In this scenario its task is solely to hide the constructor Create of TComputer. Please note that you <strong><em>MUST NOT</em></strong> use overload keyword here, or the hiding will be canceled. Now after the hiding is done, now you can freely spam your overload keyword in its descendants to get multiple constructor with same name.</p> <p>To check, you can see that the following code will not compile:</p> <pre><code> TCellPhone.Create('My Teapot'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload