[fpc-pascal]init a class from another class

Matt Emson memsom at interalpha.co.uk
Tue Oct 9 22:47:14 CEST 2001


Taking on board what Jonas has said (you have mixed Turbo Pascal syntax with 
that of Object Pascal/Delphi)..


>tCoDec = CLASS
>  constructor Create; VIRTUAL;
>  destructor Destroy; override;
>end;
>
>tmyCoDec = CLASS (tCoDec)
>  constructor create; OVERRIDE;
>  destructor Destroy; OVERRIDE;
>end;
>
>pCodecList = ^tCodecList;
>tCodecList = RECORD
>  codec :tCoDec;
>  next :pCodecList;
>end;

Does FPC have a TList? Surely that would work out better.

>tMain = CLASS
>  codec_list :pCodecList;
>  constructor create;
>  destructor destroy; override
>
>  registerCoDec (cd :tCoDec); //doesn't need 'var', all classes are passed 
by reference.
>end;
>
>tMain.registerCoDec(cd:tCoDec);
>begin
>  //cd := "class of cd".init;  why do you need this line??
>  AddToCoDecsList(cd);
>end;

Try:

var
  Main: tMain;

{...}

procedure Test;
begin
  Main.registerCoDec(tCoDec.Create);

  //or

 Main.registerCoDec(tMyCoDec.Create); 
end;

because tMyCoDec inherits from tCoDec, you may pass it as a tCoDec. You will 
then need to typecast it to get at any 'added' features, but this is normal 
even in languages such as C/C++.

>
>I don't know how to do this or if it's possible. It's the first time I use 
>classes and maybe the first I use OOP at all, so maybe this is completely 
>impossible to do.

Nope, I do this all the time. I have just written a fairly complex 
administration tool that uses small data classes to pass information around 
in a similar wat to above. In all cases the routine that accepts the data 
uses the base class as the param, so that I can typecast where needed, but 
simplify the overall routines.

Matt
-- 
---
Don't have BeOS? Get it! - http://free.be.com/
Wanna keep BeOS alive? Save BeOS - 
http://www.BeFAQs.com/save/saveform.html

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w 
O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ 
G e++ h--- r+++ y+++ 
------END GEEK CODE BLOCK------





More information about the fpc-pascal mailing list