<div dir="ltr"><div class="gmail_default" style="font-family:courier new,monospace">Hi,<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">First of all, I did some research and found this:<br>
<br><a href="http://free-pascal-general.1045716.n5.nabble.com/Compiler-Warning-Constructor-should-be-public-td2825747.html">http://free-pascal-general.1045716.n5.nabble.com/Compiler-Warning-Constructor-should-be-public-td2825747.html</a><br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace">But that did not convince me. So here is my situation and question.<br></div><div class="gmail_default" style="font-family:courier new,monospace">
<br></div><div class="gmail_default" style="font-family:courier new,monospace">I have the following case:<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">TBase = class<br>public<br></div>
<div class="gmail_default" style="font-family:courier new,monospace"> constructor Create; //VER-0<br>end;<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">TDerived = class(TBase)<br>
private<br> constructor Create(param1); //VER-1 <br>public<br> constructor Create; reintroduce; //VER-0'<br> constructor Create(param1, param2); //VER-2<br> constructor Create(param1, param2, param3); //VER-3<br>
end;<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">My purpose is that while using TDerived, only VER-2<br>and VER-3 of the constructors should be used, VER-0<br>and VER-1 are used INTERNALLY by VER-2 and VER-3 and<br>
should NOT be used publicly.<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">As restricted by the "rule of OOP", you cannot make a<br>method which is public in base class private in descendants,<br>
</div><div class="gmail_default" style="font-family:courier new,monospace">I plan to do the following:<br><br>1) reintroduce a Create (without param), which just throw<br>an exception explaining that this MUST NOT be called directly.<br>
</div><div class="gmail_default" style="font-family:courier new,monospace">Meanwhile, use "inherited Create" internally in other versions<br>of the constructors.<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">
2) Make VER-1 private so that it cannot be called outside.<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">Now the compiler throw a warning that constructor should be<br>public.<br><br></div>
<div class="gmail_default" style="font-family:courier new,monospace">In the post I referred, Graeme explained that:<br><br>==============================================================<br>Your code is flawed. Object Pascal is quite clear regarding <br>
visibility
rules. TObject has a Public constructor. You can<br>only raise the
visibility from there, not reduce visibility.
<br><br>Imagine how confusing it will be if you code was a library <br>of some sorts
and other developers had to use it. In a <br>inherited class, you have
public visibility to the constructor<br>or some other method, and then
suddenly in the descendant class<br>the methods are hidden from the
developer?? Not a good idea.
<br>==============================================================<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">I don't understand this, because to me, the VER-1 constructor is<br>
introduced in TDerived, make it non-public does NOT reduce the<br>visibility of VER-0 constructor, hence is not a violation of <br>visibility rules, is it???<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">
Thanks!<br></div><div class="gmail_default" style="font-family:courier new,monospace">Xiangrong<br></div></div>