简体   繁体   中英

Translate UniGui Delphi code to C++Builder

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM