<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Am 13.11.2013 03:35, schrieb Xiangrong
      Fang:<br>
    </div>
    <blockquote
cite="mid:CAP93jB0By6L=WpF_h1GQHaY555eHNe+=bY4L+z8V+Rcjpa0ehw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_default" style="font-family:'courier
          new',monospace">Hi All,</div>
        <div class="gmail_default" style="font-family:'courier
          new',monospace"><br>
        </div>
        <div class="gmail_default" style="font-family:'courier
          new',monospace">
          According to <a moz-do-not-send="true"
            href="http://www.freepascal.org/docs-html/ref/refsu72.html">http://www.freepascal.org/docs-html/ref/refsu72.html</a>,<br>
        </div>
        <div class="gmail_default" style="font-family:'courier
          new',monospace">
          <br>
        </div>
        <div class="gmail_default" style="font-family:'courier
          new',monospace">the overload keyword is mostly optional and
          for delphi compatiblity only. But on Oct. 27, Sven replied my
          question about hide a public constructor in base class:</div>
        <div class="gmail_default" style="font-family:'courier
          new',monospace"><br>
        </div>
        <div class="gmail_default" style="font-family:'courier
          new',monospace">
          <p style="font-family:arial,sans-serif;font-size:14px">
            <span style="color:rgb(80,0,80)">>>  I don't
              understand why this does not work:</span><br>
          </p>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px">>>
            f := <span class=""
              style="background-color:rgb(255,255,204);color:rgb(34,34,34)">TFileStream</span>.Create;<br>
            >><br>
            >> I know that <span class=""
              style="background-color:rgb(255,255,204);color:rgb(34,34,34)">TFileStream</span>'s
            Create require a file name parameter, but as <span class=""
style="background-color:rgb(255,255,204);color:rgb(34,34,34)">TFileStream</span> is</div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px">>>
            inherited from TObject, which has a Create without
            parameter.  This is why I feel that it </div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px">
            >> is possible to HIDE a public constructor from
            ancestor.</div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px"><span
              style="color:rgb(34,34,34)"><br>
            </span></div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px">
            > <span style="color:rgb(34,34,34)">No, I don't think so.
              If you want users to use the original constructor as well
              you'd need to </span></div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px">
            <span style="color:rgb(34,34,34)">> declare yours as
              "overload". If it would be possible to call</span><span
              style="color:rgb(34,34,34)"> </span><span class=""
              style="color:rgb(34,34,34);background-color:rgb(255,255,204)">TFileStream</span><span
              style="color:rgb(34,34,34)">.Create without </span></div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px"><span
              style="color:rgb(34,34,34)">> parameters then the
              instance would be in an inconsistent state, so only
              calling the</span></div>
          <div class="im"
            style="font-family:arial,sans-serif;font-size:14px">
            <span style="color:rgb(34,34,34)">> constructor with
              arguments should be possible (which is the case for</span><span
              style="color:rgb(34,34,34)"> </span><span class=""
              style="color:rgb(34,34,34);background-color:rgb(255,255,204)">TFileStream</span><span
              style="color:rgb(34,34,34)">.</span><br>
          </div>
          <p style="font-family:arial,sans-serif;font-size:14px">> So
            this is definitely by design.</p>
          <p style="font-family:arial,sans-serif;font-size:14px">Now my
            question is: in this case overload keyword DO make a
            difference? Then, how to overload? e.g.:</p>
          <p style="font-family:arial,sans-serif;font-size:14px">In base
            class:</p>
          <p style="font-family:arial,sans-serif;font-size:14px">constructor
            Create;</p>
          <p style="font-family:arial,sans-serif;font-size:14px">In
            child class:</p>
          <p style="font-family:arial,sans-serif;font-size:14px">constructor
            Create(param1:...; param2: ...): overload;</p>
          <p style="font-family:arial,sans-serif;font-size:14px">i.e.
            overload does NOT require functions to have same parameter
            list or return type? Also, it does not require the overload
            keyword in base class? Otherwise programmers need to modify
            the base class, which is not good.</p>
        </div>
      </div>
    </blockquote>
    Take a look at this example:<br>
    <br>
    === code begin ===<br>
    <br>
    program toverload;<br>
    <br>
    {$mode objfpc}<br>
    <br>
    type<br>
      TTest = class<br>
        constructor Create(aArg1: Integer);<br>
      end;<br>
    <br>
    constructor TTest.Create(aArg1: Integer);<br>
    begin<br>
    <br>
    end;<br>
      <br>
    var<br>
      t: TTest;<br>
    begin<br>
      t := TTest.Create;<br>
    <br>
    end.<br>
    <br>
    === code end ===<br>
    <br>
    This will fail to compile, because TTest.Create will hide the
    default constructor of TObject (which takes no arguments). If you
    now add "overload" the code will compile. So you can control whether
    inherited methods with different arguments (return type does not
    count!) is visible in your derived class. If the method in the base
    and the child have the same argument list than the one in the child
    will always hide the one in the base.<br>
    You don't need to add "overload" in the base class (here TObject).
    The same works for global functions/procedures in units as well btw.<br>
    <br>
    The other use of overload is the one where Delphi requires
    "overload" and FPC allows it optionally (except in mode Delphi where
    it is required): to declare to functions/procedures in the same unit
    or two methods in the same class with different argument lists.<br>
    <br>
    I hope this explains :)<br>
    <br>
    Regards,<br>
    Sven<br>
  </body>
</html>