1
votes

Hi I have one question regarding some type casting approaches. I am translating some Delphi files to C++. I have delphi declaration of class which is derived from TList and it's a base class for other derived classes.

type TBaseItem = class (TObject)
public
  procedure SomeProcedure; virtual; abstract;
end;

Type TBaseClass = class(TList)
private 
  function GetItem(Index: Integer): TBaseItem;
  procedure SetItem(Value: TBaseItem; Index: Integer);
public
  property Items[Index: Integer]: TBaseItem read GetItem write SetItem;
end;


function TBaseClass.GetItem(Index: Integer): TBaseItem;
begin
  Result := TBaseItem(inherited Items[Index]);
end;

procedure TBaseClass.SetItem(Value: TBaseItem; Index: Integer);
begin
  inherited Items[Index] := Value;
end;

This are two base classe TBaseItem and TBaseClass. So here are declared new classes TchildItem which is derived from TBaseItem and TChildClass which is derived from TBaseClass. TChildItem is overriding method SomeMethod and what is more important is that TChildtClass is overriding property Items in a way that now we are returning TParentItem items insted of TBaseItem.

type TChildItem = class (TBaseItem)
public
  procedure SomeProcedure; override;
end;

type TChildClass = class(TBaseClass)
private 
  function GetItem(Index: Integer): TChildItem;
  procedure SetItem(Value: TChildItem; Index: Integer);
public
  property Items[Index: Integer]: TChildItemread GetItem write SetItem;
end;

function TChildClass .GetItem(Index: Integer): TChildItem;
begin
  Result := TChildItem(inherited Items[Index]);
end;

procedure TChildClass.SetItem(Value: TChildItem; Index: Integer);
begin
  inherited Items[Index] := Value;
end;

With this example I wanted to show how easy can be done deriving classes and overriding properties. Getting proper type of item out of a list is simply done by calling parents (base) property Item and typecast it to proper type. This is delphi apporach.

I wonder how can I translate this part of code to C++. Currently I declared a new base class which is not derived from any class and it has public var Items which is

class TBaseItem{
  virtual void SomeMethod();
}

class TBaseClass {
public:
  vector<TBaseItem> Items; 
};


class TChildItem : public TBaseItem{
}

class TChildClass : public TBaseClass {
};

and then use

return (TChildItem) Items[Idx]

That means I would like to access parent's (TBaseClass) public variables such as that vector Items and typecast it to proper type... My first impression is that I might be going into wrong direction with that Delphi approach.

What do you suggest? How should I go with that?

THANK YOU VERY MUCH!

1
Trying to apply "good practice" of one language in another, is almost always a bad idea, especially when languages differ so much. I don't really get what you want to do, but if you just want to create some kind of container with different possible types of elements, try having a look at templates.PlasmaHH

1 Answers

5
votes

The Delphi code is old and pre-dates generics, the Delphi analogue to C++ templates. In modern Delphi code those list classes would simply not exist. Instead one would use TList<TBaseItem> and TList<TChildItem>.

In C++ code you would simply use vector<TBaseItem*> and vector<TChildItem*>. There is simply no point in your C++ translation to implement TBaseClass and TChildClass.

I would also correct your terminology. Delphi properties cannot be overriden. The new property in TChildClass is just that, a new property.