[fpc-pascal] Understanding virtual methods
flavio.etrusco at gmail.com
Tue Aug 20 09:36:11 CEST 2013
On Tue, Aug 20, 2013 at 2:34 AM, Xiangrong Fang <xrfang at gmail.com> wrote:
> Hi Flavio,
> Your findings confirmed mine, but not telling me why?
I don't know why, maybe FPC used to be more strict about the use of
inherited, or the docs are simply wrong.
> It seems that the "virtual" keyword has no use at all!
It does, and the documentation is correct in this respect.
> To confirm this, I just removed the
> "inherited" call in TDerived, then re-run the program with or without
> "virtual/override", the result is exactly same, i.e. with c2 (declared as
> TBase), the following statements ALWAYS calls constructor of TDerived, NOT
> c2 := TDerived.Create;
> c2 := TBase(TDerived.Create);
As I said - or tried to say ;) - in the other post, there won't be a
difference when invoking the constructor on a "class literal"
The second line will generate the exact same code as the first one;
you're instantiating a TDerived instance then "upcasting" it to TBase.
> This is not same as the description in:
Note this page is about methods and you're using constructors in your example.
I would recommend you first do your tests on method declarations, and
later learn about class references (i.e. "class of" declarations) and
> BTW, the above documents are talking about objects, but I am using classes,
> is there any difference here?
AFAIU per the first page you sent, you can't shadow/reintroduce a
virtual method in an object; when you redeclare a virtual method it
overrides the base one.
More information about the fpc-pascal