GetPointer

Overview

The GetPointer function returns the object pointer for a given class object. It is crucial for managing object references in MQL4.

Signature

void* GetPointer(any_class anyobject);

Parameters

  • anyobject [in]: An object of any class for which to retrieve the pointer.
  • Return Value

  • Returns the object pointer (void*).
  • Details

  • **Pointer Applicability**: Pointers are only applicable to class objects. Instances of structures and simple-type variables cannot have pointers and attempting to use GetPointer on them will result in a compiler error.
  • **Pointer Types**:
  • * Objects created using the new() operator yield dynamic pointers. * Objects automatically created (e.g., within an array) yield POINTER_AUTOMATIC type pointers. These pointers cannot be used with the delete() operator.
  • **Pointer Validity**: Pointers obtained from GetPointer can be invalid. An invalid pointer can be NULL or point to an object that has been deleted. Using an invalid pointer leads to critical program termination.
  • **Safety**: Always use CheckPointer() before dereferencing a pointer to ensure its validity and prevent runtime errors.
  • Constraints

  • Prohibited to apply GetPointer() to structure types or simple types.
  • Prohibited to pass a pointer as a function argument.
  • An attempt to call an incorrect pointer causes critical termination.
  • Related Functions

  • CheckPointer(): Used to verify pointer validity.
  • delete(): Operator for deallocating dynamically allocated memory.
  • Example

    //+------------------------------------------------------------------+
    //| Check_GetPointer.mq5                                             |
    //| Copyright 2009, MetaQuotes Software Corp.                        |
    //| https://www.mql5.com                                             |
    //+------------------------------------------------------------------+
    #property copyright "2009, MetaQuotes Software Corp."
    #property link     "https://www.mql5.com"
    #property version   "1.00"

    //+------------------------------------------------------------------+ //| Class implementing the list element | //+------------------------------------------------------------------+ class CItem { int m_id; string m_comment; CItem* m_next; public: CItem() { m_id=0; m_comment=NULL; m_next=NULL; } ~CItem() { Print("Destructor of ",m_id, (CheckPointer(GetPointer(this))==POINTER_DYNAMIC)? "dynamic":"non-dynamic"); } void Initialize(int id,string comm) { m_id=id; m_comment=comm; } void PrintMe() { Print(__FUNCTION__, ":",m_id,m_comment); } int Identifier() { return(m_id); } CItem* Next() { return(m_next); } void Next(CItem *item) { m_next=item; } }; //+------------------------------------------------------------------+ //| Simplest class of the list | //+------------------------------------------------------------------+ class CMyList { CItem* m_items; public: CMyList() { m_items=NULL; } ~CMyList() { Destroy(); } bool InsertToBegin(CItem* item); void Destroy(); }; //+------------------------------------------------------------------+ //| Inserts list element at the beginning | //+------------------------------------------------------------------+ bool CMyList::InsertToBegin(CItem* item) { if(CheckPointer(item)==POINTER_INVALID) return(false); //--- item.Next(m_items); m_items=item; //--- return(true); } //+------------------------------------------------------------------+ //| Deletes the list by deleting elements | //+------------------------------------------------------------------+ void CMyList::Destroy() { //--- CItem* item; //--- while(CheckPointer(m_items)!=POINTER_INVALID) { item=m_items; m_items=m_items.Next(); if(CheckPointer(item)==POINTER_DYNAMIC) { Print("Dynamyc object ",item.Identifier()," to be deleted"); delete (item); } else Print("Non-dynamic object ",item.Identifier()," cannot be deleted"); } //--- } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CMyList list; CItem items[10]; CItem* item; //--- item=new CItem; if(item!=NULL) { item.Initialize(100,"dynamic"); item.PrintMe(); list.InsertToBegin(item); } //--- for(int i=0; i<10; i++) { items[i].Initialize(i,"automatic"); items[i].PrintMe(); item=GetPointer(items[i]); if(CheckPointer(item)!=POINTER_INVALID) list.InsertToBegin(item); } //--- item=new CItem; if(item!=NULL) { item.Initialize(200,"dynamic"); item.PrintMe(); list.InsertToBegin(item); } //--- list.Destroy(); //--- } //+------------------------------------------------------------------+

    See Also

  • [Object Pointers](/basis/types/object_pointers)
  • [Checking the Object Pointer](/constants/namedconstants/enum_pointer_type)
  • [Object Delete Operator delete](/basis/operators/deleteoperator)