Class objects and arrays and pointers

 

I really don't understand arrays of class objects.

If we look at this page we see the correct way to use an array of objects is to use pointers

https://www.mql5.com/en/articles/351

CName* cname[10]

but my working code doesn't do that, it uses an array like this 

MyClass objs[];

and my code works but I'm trying to debug some object leaks.

if I try to change my code to 

MyClass* objs[];

then it will compile fine but then I will get a runtime error about "invalid pointer access"

on my code (that works with the non-pointer array definition above): objs[j++] = obj; (this line above is obj=new MyClass(); )

so I'm stuck here. I'm especially confused why my code works even though I'm not following the pointer array declartion method the docs advocate.

also, the objects in the array (in my working non pointer code) are the only ones of the MyClass type defined anywhere with a "new". But for some reason when the EA is unloaded I get an error message about "x objects of MyClass left", but my understanding is that arrays of objects automatically dispose of these objects within when they are disposed? the array belongs another class (MyClasses) with a global instance and that class in the OnDeInit explicitly has a "delete" called for that instance. So the instance of MyClasses should delete automatically all the MyClass objects in its array field, upon this delete being used on the MyClasses instance, correct? so I'm scratching my head over where this leak is coming from. anyone have any ideas?

The Basics of Object-Oriented Programming
The Basics of Object-Oriented Programming
  • www.mql5.com
We can assume that anyone who tried to start learning object-oriented programming (OOP), first encountered such words as polymorphism, encapsulation, overload and inheritance. Maybe someone looked at some ready-made classes and tried to figure out where those polymorphism or encapsulation actually are... Most likely this can be the end of OOP...
 

An array of objects and an array of pointers are two completely different things.

If you use pointers, you must create them (new object) and on deinit, delete them. You are responsible for their life time.

If you use an array, they are created (via the default constructor) as the array grows (or initial creation,) and deconstructed as the array shrinks (or goes out of scope.)

My advice is, unless you are using MT CList or MT's sort method (CObject,) don't use pointers.
          pass generic class data field - Swing Trades - MQL4 programming forum

 
William Roeder:

An array of objects and an array of pointers are two completely different things.

If you use pointers, you must create them (new object) and on deinit, delete them. You are responsible for their life time.

If you use an array, they are created (via the default constructor) as the array grows (or initial creation,) and deconstructed as the array shrinks (or goes out of scope.)

My advice is, unless you are using MT CList or MT's sort method (CObject,) don't use pointers.
          pass generic class data field - Swing Trades - MQL4 programming forum

could it be that back in 2011 when the article I included was written that an array of pointers was the only way to create an array for objects? I'm just confused why they would talk about using pointers in an introductory article in the section called "Array of Objects".. 

Reason: