Download MetaTrader 5

ArrayCopy array of pointers

To add comments, please log in or register
Quintos
16
Quintos  

Hello,


I want to use ArrayCopy() on an array of pointers but it looks like i'm not allowed. It generates an error saying that array cannot contain objects. Is it not possible to use ArrayCopy() with arrays containing object pointers?


MyObject* arrTarget[];

void Copy(MyObject* &arrSource[]) {
        ArrayResize(arrTarget, ArraySize(arrSource));

        // Is this not allowed?
        ArrayCopy(arrTarget, arrSource);
}

Docs say "Array of classes and structures containing objects that require initialization aren't copied". But nothing about pointers.


thnx.

Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference
Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference
Juan Fernandez
3459
Juan Fernandez  

With b1650 works without problems. I don't know what changes they made in last builds but some things didn't work before now works well

nicholishen
911
nicholishen  

Here is what I use for pointers. It's a small modification of CArrayObj which allows you to use the [] operator on the array of objects. Here is an example. 

#include <Arrays\objvector.mqh>

class MyClass : public CObject
{
protected:
   int      m_index;
public:
            MyClass(int index):m_index(index){}
   MyClass* Print()  {::Print    ("index = "+string(m_index));return &this;}
   MyClass* Alert()  {::Alert    ("index = "+string(m_index));return &this;}
   MyClass* Comment(){::Comment  ("index = "+string(m_index));return &this;}
};

void OnStart()
{
   objvector<MyClass*> src_arr, dst_arr;
   
   for(int i=0;i<5;i++)
      src_arr.Add(new MyClass(i));
   
   dst_arr.AssignArray(&src_arr);
   
   for(int i=0;i<dst_arr.Total();i++)
      dst_arr[i].Print().Alert().Comment();
   
}
//+------------------------------------------------------------------+
Just remember if you use CArrayObj that it manages memory by default so as soon as the objvector gets destroyed it also destroys all objects it points to. To turn off the memory mgmt you want to use 
src_arr.Freemode(false);
Files:
To add comments, please log in or register