MQL4 Object Pointers

Overview

MQL4 supports dynamic object creation using the new operator, which returns an 8-byte object descriptor (syntactically similar to C++ pointers). These descriptors are not direct memory pointers. All objects in function parameters must be passed by reference.

Dynamic Object Creation and Management

  • **Creation:** Use the new operator to create objects dynamically. Example: MyObject* hobject = new MyObject();
  • **Deletion:** Objects created with new must be explicitly deleted using the delete operator before termination to prevent memory leaks. Example: delete(foo2);
  • Object Passing

  • **By Reference:** Objects are always passed by reference to functions. Example: void PrintObject(Foo &object)
  • **Pointers to Objects:** Pointers to objects can also be passed. The compiler automatically converts pointers to object references when needed. Example: PrintObject(foo2);
  • Arrays

  • **Arrays of Objects:** Can be declared and passed to functions. Example: Foo foo_objects[5];
  • **Arrays of Pointers:** Can be declared, populated with dynamically created objects, and passed to functions. Example: Foo* foo_pointers[5];
  • Key Differences from C++

  • MQL4 descriptors are not direct memory pointers.
  • All function parameters involving objects are passed by reference.
  • Related Concepts

  • new operator
  • delete operator
  • Pass-by-reference
  • Arrays
  • User-defined Types
  • this keyword