Skip to content
All library documents

MQL5 Object Pointers, Safety Checks, and Virtual Methods

Article MQL5 articles

Summary

This article explains the difference between automatically created MQL5 objects and objects created dynamically through pointers. It demonstrates that using an uninitialized object pointer can terminate a program, then shows how CheckPointer can detect an invalid pointer before calling a method. It also examines passing an object by reference, where checking the referenced object after the call is not a safe substitute for validating a pointer first.

The discussion extends to arrays of base-class objects and polymorphism: a base-class method needs to be declared virtual for calls to use the derived object’s implementation at runtime. Examples use small classes and scripts rather than trading systems, so the lessons apply to MQL5 program design generally. The conclusion recommends automatic object creation unless pointers are needed, checking pointer validity before use, and remembering that MQL5 pointers are not equivalent to C++ memory pointers and should not be passed to DLLs as such.

Key ideas

  • MQL5 objects can be created automatically or dynamically through an object pointer.
  • Calling a method through an invalid pointer can cause a critical program error.
  • CheckPointer can verify pointer validity before the program uses the referenced object.
  • A base-class method must be virtual for derived implementations to dispatch at runtime.
  • MQL5 pointers differ from C++ memory pointers and should not be passed to DLLs as ordinary pointers.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.