Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-10

init a class from another class

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ivan Montes

Hi all,

I cannot find a solution to a problem I've got programming a library. I want to initialze a class which is passed as a parameter to another class method. It looks like this :

tCoDec = CLASS
  constructor init; VIRTUAL;
  destructor done; VIRTUAL;
end;
tmyCoDec = CLASS (tCoDec)
  constructor init; OVERRIDE;
  destructor done; OVERRIDE;
end;
pCodecList = ^tCodecList;
tCodecList = RECORD
  codec :tCoDec;
  next :pCodecList;
end;
tMain = CLASS
  codec_list :pCodecList;
  constructor init;
  destructor done;
  registerCoDec (var cd :tCoDec);
end;
tMain.registerCoDec(var cd:tCoDec);
begin
  cd := "class of cd".init;
  AddToCoDecsList(cd);
end;

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.

Any help will be appreciated, thks so much.
bye, Ivan

Jonas Maebe
to Ivan Montes
Ivan Montes wrote
tCoDec = CLASS   constructor init; VIRTUAL;   destructor done; VIRTUAL; end;

When using classes, you should use "create" as constructor name and you *must* use "destroy" as destructor name (if you don't do the latter, the destructor won't be called automatically when the class instance is freed). Also, your destructor should be declared as "override", because the destructor over TObject (of which all classes are derived) is already virtual. Have a look at the manuals to see the differences between "virtual" and "override" (in short: the first time a virtual method appears in a class hierarchy, you should declare it as "virtual" and afterwards, when you want to override it in a child class, use "override" instead).

Add the following to your program for the solution:

tTypeOfCoDec = class of tCoDec;
tMain.registerCoDec(var cd: tCoDec; mytype: tTypeOfCoDec);
begin
   cd := mytype.create;
  AddToCoDecsList(cd);
end;

Jonas

Michael Van Canneyt
to Matt Emson: Taking on board what Jonas has said (you have mixed Turbo Pascal syntax with that of…
Matt Emson wrote 21 lines
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.

It does, see the Classes unit.

Michael.

Matt Emson
to Ivan Montes: I cannot find a solution to a problem I've got programming a library. I want to initialze…

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

Show quoted text 15 lines
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.

Show quoted text 6 lines
tMain = CLASS codec_list :pCodecList; constructor create; destructor destroy; override registerCoDec (cd :tCoDec); //doesn't need 'var', all classes are passed

by reference.

Show quoted text 7 lines
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------