0

How can I translate this code into C++? UniGui component.

var
  FrmList : TList;
  I : Integer;
  Frm : TUniBaseForm;
begin
  FrmList := UniSession.FormsList;

  UniListBox1.Clear;
  for I := 0 to FrmList.Count - 1 do
  begin
    Frm := FrmList[I];
    UniListBox1.Items.Add(Frm.Name + ' ' + Frm.ClassName  );
  end;

I have a problem with Frm := FrmList[i];. I have tried:

frm = UniSession->FormsList->Items[i];

E2034 Cannot convert 'void *' to 'TUniBaseForm *'

frm = dynamic_cast<TUniBaseForm*>(UniSession->FormsList->Items[i]);

E2307 Type 'void' is not a defined class with virtual functions

frm = dynamic_cast<TUniBaseForm*>(UniSession->FormsList[i]);

E2031 Cannot cast from 'TList' to 'TUniBaseForm *'

Arioch 'The
  • 15,799
  • 35
  • 62
antarey
  • 29
  • 5
  • `(void *) frm = ...` ? Or C does not allow datatyping LValues? Also, https://www.tutorialspoint.com/cprogramming/c_unions.htm – Arioch 'The Jul 30 '20 at 14:18

1 Answers1

1

Delphi allows an untyped pointer (void* in C++) to be assigned to another typed pointer without a cast. C++ does not, you need an explicit cast.

You were on the right track with your 2nd attempt, but you need to use either static_cast or reinterpret_cast, instead of dynamic_cast, eg:

TUniBaseForm *Frm = static_cast<TUniBaseForm*>(FrmList->Items[i]);

or

TUniBaseForm *Frm = reinterpret_cast<TUniBaseForm*>(FrmList->Items[i]);

See these related questions:

Should I use static_cast or reinterpret_cast when casting a void* to whatever

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770