[fpc-pascal] Understanding virtual methods

Antonio Fortuny a.fortuny at sitasoftware.lu
Tue Aug 20 09:35:40 CEST 2013


Le 20/08/2013 07:34, Xiangrong Fang a écrit :
> Hi Flavio,
>
> Your findings confirmed mine, but not telling me why? It seems that 
> the "virtual" keyword has no use at all!   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 TBase:
>
> c2 := TDerived.Create;
> c2 := TBase(TDerived.Create);
>
> This is not same as the description in: 
> http://www.freepascal.org/docs-html/ref/refsu26.html
>
> BTW, the above documents are talking about objects, but I am using 
> classes, is there any difference here?
Yep: a class is a construct definig a collection of variables, 
properties and methods whilst an object is an instantiated class.

TMyClass = class(TSomething)            <<-------- this is a class
public
     constructor Create;
     blabla
end;

var
     obj: TMyClass ;        <<----------- this is a declared oblect 
which instantiates a class when created (see next line)
...
     Obj := TMyClass .Create;            <<----- now the class TMyClass 
exists thru the Obj object

In other words, a class is non existent whilst an object falls into the 
real world. A class describes, an object makes use of a class
A class is unique bet there can be lots of objects instantiating a class
These few words help to explain the basic difference even if some more 
technical explations couls be needed. AFAIK, all OOP languages use 
almost the same terminology

This is why class procedures and functions exist. But this is another story.

Antonio.
>
> Shannon
>

-- 
Sita Software
*Antonio Fortuny*
Senior Software engineer

220, avenue de la Liberté
L-4602 Niederkorn
Tel.: +352 58 00 93 - 93
www.sitasoftware.lu <http://www.sitasoftware.lu>
Your IT Partner

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130820/008aed58/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: iijjjbdj.jpg
Type: image/jpeg
Size: 2306 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130820/008aed58/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fbehfhcg.jpg
Type: image/jpeg
Size: 921 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130820/008aed58/attachment-0001.jpg>


More information about the fpc-pascal mailing list