MQL4 Typecasting

Numeric Typecasting

  • **Allowed Casting:** Visual schemes illustrate allowed conversions between numeric types. Solid lines with arrows indicate conversions with minimal information loss. Dashed lines indicate conversions with potential precision loss.
  • **Integer to Float:** Conversions from integer to float maintain order but may lose accuracy (e.g., int to float).
  • **Float to Integer:** Fractional part is deleted. Use MathRound() for rounding to the nearest whole number.
  • **Binary Operations:** Operands are converted to the higher type based on priority before operation execution. char, uchar, short, ushort are unconditionally converted to int.
  • **Explicit Casting:** Uses (type)variable or type(variable) syntax. Example: double d2=(double)c1/2+0.3;.
  • **Data Loss/Truncation:** Casting can result in values outside the permissible range, leading to truncation (e.g., char c=400; results in c=-112).
  • **Assignment Operations:** Data is cast to the target type before assignment.
  • **long/ulong to double:** Precision loss can occur if the integer value is outside the range [-9223372036854774784, 9223372036854774784].
  • String Typecasting

  • **High Priority:** String type has the highest priority; other operands in operations with strings are automatically cast to string.
  • **Addition Operator:** Only the + operator is supported for strings.
  • **Explicit Casting:** Explicit casting of strings to numeric types is allowed (e.g., bool(str1), color(str2)).
  • **NULL Constant:** string s2=NULL; deinitializes a string, resulting in an empty string.
  • Simple Structure Typecasting

  • **Assignment:** Data of simple structure types can be assigned to each other if all members are numeric. A simple byte-wise copy is performed.
  • **Size Difference:** If structures differ in size, the number of bytes of the smaller size is copied.
  • **Example:** Structures of different types (str1, str2, str3) can be assigned if their member types allow byte-wise copying.
  • **RGB Example:** Demonstrates using structures of the same size but different member types (e.g., color vs. RGB with uchar components) for data interpretation via byte-wise copying.
  • Base Class Pointers to Derived Class Pointers

  • **Implicit Conversion:** Derived class objects can be viewed as base class objects.
  • **Explicit Casting:** Base class pointers can be explicitly cast to derived class pointers. Requires full confidence; otherwise, a critical runtime error occurs.
  • Dynamic Typecasting (dynamic_cast)

  • **Runtime Check:** dynamic_cast operator performs type validation at runtime for pointers to classes.
  • **Syntax:** dynamic_cast (expression).
  • **Result:** If the conversion is invalid, dynamic_cast returns NULL. The type-id must be a previously defined class type. The expression operand can be any value except void.
  • **Example:** CFoo *foo = dynamic_cast(&bar); where bar is of type CBar and CFoo inherits from CBar. If bar is not actually a CFoo object, foo will be NULL.