将 UniGui Delphi 代码翻译成 C++Builder

Translate UniGui Delphi code to C++Builder

如何将这段代码翻译成 C++? UniGui 组件。

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;

我对 Frm := FrmList[i]; 有疑问。我试过:

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 *'

Delphi 允许将无类型指针(C++ 中的 void*)分配给另一个无类型指针而无需强制转换。 C++ 没有,你需要显式转换。

您第二次尝试的方向是正确的,但您需要使用 static_castreinterpret_cast,而不是 dynamic_cast,例如:

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

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

查看这些相关问题:

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?