C++ Builder: Type-castingTypecasting is making a variable of one type act like another type. For example, you might have a float that you need to use in a function that requires an integer. Implicit conversionImplicit conversions are automatically done when a variable is copied to a variable of a
compatible type.
Here, the variable A has been typecast from float to int. We didn't have to specify any
explicit typecasting. Explicit conversionMany conversions require an explicit conversion. For example, you might have a float that you need to use in a function that requires an integer:
Dynamic castDynamic cast is often used for objects of which the type is only known when the application is
running.
But you don't know its class yet: is it a TLabel, a TButton, a TPanel...? Here, the dynamic cast comes to the rescue. Basic syntax: Object2 = dynamic_cast<T> (Object1) where T is a class type. If Object1 is not of class type T, this results in the value NULL. void __fastcall TForm1::Button1Click(TObject *Sender) { int i; TPanel *Panel; for (i = 0; i << ComponentCount; i++) { Panel = dynamic_cast<TPanel*>(Components[i]); if (Panel != NULL) Panel->Caption = ""; } } |