Autor Beitrag
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 20.12.05 16:14 
Hi!

Folgendes Problem: ich habe eine Baisklasse, die den Konstruktor ComeToLife implementiert. In der Kinderklasse soll dieser Konstruktor erweitert werden. Allerdings scheint sich irgendwie ein logischer Fehler eingeschlichen zu haben. Jemand eine Idee, wie ich das ohne Compilerfehler lösen kann ;)?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
{..}
  TCustomLivingBeing = class
    protected
      FAge: Word;
      FSex: TLivingBeingSex;
    public
      constructor ComeToLife(Parents: Array of TCustomLivingBeing);
      destructor Die; virtual; abstract;
{..}
    end;

  TLivingBeing = class(TCustomLivingBeing)
    public
      constructor ComeToLife(Parents: Array of TLivingBeing; AName: String);
    end;

implementation

constructor TCustomLivingBeing.ComeToLife(Parents: Array of TCustomLivingBeing);
begin
  FAge := 0; //Set age 0 at birth
  FSex := Parents[Random(High(Parents) + 1)].Sex; //Random sex of one of the parents
end;

constructor TLivingBeing.ComeToLife(Parents: Array of TLivingBeing; AName: String);
begin
  inherited ComeToLife(Parents);
end;


AXMD
petergensfeld
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46

Windows Vista 32-Bit
Delphi XE Professional
BeitragVerfasst: Di 20.12.05 16:38 
Was meckert denn der Compiler?
AXMD Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 20.12.05 16:41 
Inkompatible Typen in der markierten Zeile. Mir ist eigentlich klar warum, die Frage ist nur, wie man das am besten lösen kann.

AXMD
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Di 20.12.05 17:20 
Du musst auch im Konstruktor von TLivingBeing den Typ TCustomLivingBeing verwenden.
Dort kannst du dann mit IS abfragen, ob der Typ stimmt und danach mit AS typecasten.

Beste Grüße
Tastaro
petergensfeld
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46

Windows Vista 32-Bit
Delphi XE Professional
BeitragVerfasst: Di 20.12.05 17:20 
Deklariere doch den Parameter als "Array of TCustomLivingBeing". Kannst Du dann innerhalb des Konstruktors bei Bedarf wieder mit Parents[Index] as TLivingBeing casten (am besten aber vorher Typkontrolle mit "is").

Gruß Dirk


// Edit: Sorry, Testaro war um Sekunden schneller... ;)
AXMD Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 20.12.05 17:22 
Stimmt... hab es testweise mit einem neuen Array vom Typ Array of TCustomLivingBeing gelöst, dem ich alle Einträge vom Parent-Array übergebe. //EDIT: Ich werde diese "testweise" Lösung beibehalten, und das aus folgendem Grund: die Parents von einem TLivingBeing müssen TLivingBeings sein - denn ich muss auch noch Haard-/Augenfarbe... der Eltern berücksichtigen und daher ist es notwendig, dass die Eltern diese Eigenschaften aufweisen, sprich TLivingBeings sind.

AXMD
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 20.12.05 19:35 
Nur mal so eine Frage nebenbei: Deine jetzige Variante lässt doch mehr als 2 Elternteile (bzw. auch keine Eltern) zu ... Gibt es da biologische Vorbilder? :mrgreen:

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
AXMD Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 20.12.05 19:37 
user profile iconBenBE hat folgendes geschrieben:
Nur mal so eine Frage nebenbei: Deine jetzige Variante lässt doch mehr als 2 Elternteile (bzw. auch keine Eltern) zu ... Gibt es da biologische Vorbilder? :mrgreen:


Nein, reine Fantasie :mrgreen:

AXMD