[fpc-pascal] Virtual object methods

Stefan V. Pantazi svpantazi at gmail.com
Thu Jun 27 22:42:30 CEST 2019


Read down the first page, the section "Objects - Dynamic Variables" - 
all will become clear.

By avoiding the virtual methods, you can keep your objects static, all 
stored on stack, no need for constructors. I personally find very useful 
for my projects.

Find fixed example below. Hope it helps,

Stefan

// http://wiki.freepascal.org/Programming_Using_Objects
//Objects - Dynamic Variables

type
   TA = object
     constructor Init();
     procedure DoThis; virtual;
   end;

procedure TA.DoThis;
begin
   writeln('TA.DoThis');
end;

type
   PTB=^TB;
   TB = object (TA)
     procedure DoThis; virtual;
   end;

procedure TB.DoThis;
begin
   writeln('TB.DoThis');
end;

constructor TA.Init();
begin
   inherited;
end;

var

   obj: PTB;
begin
   obj:=New(PTB,Init());
   obj^.DoThis;
end.

On 6/27/19 4:17 PM, Ryan Joseph wrote:
> Why do I get a runtime error with this?
> 
> ==============================
> 
> {$mode objfpc}
> 
> program test;
> 
> // http://wiki.freepascal.org/Programming_Using_Objects
> // http://wiki.freepascal.org/Programming_Using_Objects_Page_2
> 
> type
>    TA = object
>      procedure DoThis; virtual;
>    end;
> 
> procedure TA.DoThis;
> begin
>    writeln('TA.DoThis');
> end;
> 
> type
>    TB = object (TA)
>      procedure DoThis; virtual;
>    end;
> 
> procedure TB.DoThis;
> begin
>    writeln('TB.DoThis');
> end;
> 
> var
>    obj: TB;
> begin
>    obj.DoThis;
> end.
> 
> 
> Regards,
> 	Ryan Joseph
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> 

-- 
_______________________________________________________


More information about the fpc-pascal mailing list