Generic Class Library - bugs, description, questions, usage and suggestions - page 25

 
Andrey Pogoreltsev:

Just why create collections and put them in a kodobase if they are only good for embedded types)?

They're not good for embedded structures either.

 
Andrey Pogoreltsev:

Tell me, maybe I don't understand something, but if I try to use a construction of this type:

I get an error:

'Option' - objects are passed by reference only ICollection.mqh 14 18

and there's a full stack of errors next...

This will not work, through a template template need to do, I also worked with this issue, here is a test example, it seems to work correctly

class CData - structure that we want to store in the list

CDataBase - the list itself

#property strict
#include <Arrays\List.mqh>

//+------------------------------------------------------------------+
class CData : public CObject
  {
public:
   int               x;
   double            y;
   datetime          t;
  };
//+------------------------------------------------------------------+
template<typename TDB>class CDataBase
  {
private:
   CList            *mlist;
   TDB              *TDBptr;
public:
   void CDataBase()           { mlist=new CList;                                       }
   void ~CDataBase(void)      { delete mlist;                                          }
   int ArraySize(void)        { return(mlist.Total());                                 }
   TDB *operator[](int index) { return(mlist.GetNodeAtIndex(index));                   }
   void  AddValue (TDB &value){ TDBptr = new TDB; TDBptr  = value; mlist.Add(TDBptr);  }
   string TypeName()          { return(typename(TDB));                                 }
   };
//+------------------------------------------------------------------+
// проверка, запишем  распринтуем значения
void OnStart()
  {

   CDataBase<CData>*data=new CDataBase<CData>;
   CData *my=new CData;

   int i;
   Print("1------------------------------------");
   for(i=0; i<10; i++)
     {
      my.x=i;
      my.y= i*2;
      data.AddValue(my);
      Print(i," : ",data[i].x," , ",data[i].y," / ",my.x," ,",my.y);
     }
   Print("2------------------------------------");
   for(i=0; i<data.ArraySize(); i++)
     {
      Print(i," : ",data[i].x," , ",data[i].y);
     }
   Print(data.TypeName());
   delete my;
   delete data;
  }
//+------------------------------------------------------------------+

In OnStart() - we create a list, write values to it, and then read them. I check it 2 times, because at first somewhere i lost visibility of a local variable when writing to the list - i used to write everything ok, but when i read, then i got null pointers and an error

 
Igor Makanu:

It won't work that way, you have to use templates template, I was dealing with this issue too, I made a test example, it all seems to work correctly

class CData - the structure that we want to store in the list

CDataBase - the list itself

In OnStart () - create a list, write values to it and then read them. I check it twice, because first I've lost somewhere visibility of a local variable when writing to the list - I was writing all ok, but when I was reading, then I got null pointers and an error

Your code works because CData is still a class, not a structure. If you try to use the generator for classes and structures at once, you will get problems, especially with the delete operator. I was convinced of it through my own experiments with this "generic". The point is that this "generic" library doesn't have delete operator at all and if you add a new class to such a "collection", after quitting the program there will be a lot of lost objects. I.e. it is obvious that originally this generic was written only for primitive types.

 
Vasiliy Sokolov:

This code works for you because CData is a class, not a structure. If you try to use generic for classes and structures at the same time, you will immediately encounter problems, especially with the delete operator. I was convinced of it through my own experiments with this "generic". The point is that this "generic" library doesn't have delete operator at all and if you add a new class to such a "collection", after quitting the program there will be a lot of lost objects. I.e. you can see that originally this generic was written only for primitive types.

I've stopped using structures in MQL, the structure has no advantages, but the constant bugs and time wasting while working with the structures they will provide - I read the old admins' posts with questions about the structures and they would basically say use the class instead of the structure - I don't use structures at all now

SZZ: I found this when I stopped using structureshttps://www.mql5.com/ru/forum/6343/page866#comment_7541747

Вопросы от начинающих MQL5 MT5 MetaTrader 5
Вопросы от начинающих MQL5 MT5 MetaTrader 5
  • 2018.05.23
  • www.mql5.com
Подскажите пожалуйста, такой показатель тестера в жизни реален? И хороший это или плохой результат за год с депо 3000...
 
Andrey Pogoreltsev:

why create collections and put them in a kodobase if they are only good for embedded types)?

Convenience that come as standard. Hence, no need to pull - every user has one.

I use this in one of my KB libs for long.

 
Igor Makanu:

It won't work that way, you have to use templates template, I was dealing with this issue too, I made a test example, it all seems to work correctly

class CData - the structure that we want to store in the list

CDataBase - the list itself

In OnStart () - create a list, write values to it and then read, 2 times I check it, because at first somewhere i lost visibility of a local variable when writing to the list - i used to write everything ok, but when i read, then i got null pointers read and got an error

Well first of all you implemented your allocator over a list and store pointers there. Not only that, you have a leak when destroying).

Second, you should have correctly used the copy constructor instead of the assignment operator. But this all is IMHO)

And most importantly: developers just need to finalize generics, make allocators inside and support custom objects. C++ already invented everything for them) And we don't have to invent bicycles.

 
Andrey Pogoreltsev:

Well first of all you have implemented your allocator over a list and store the pointers there. Not only that, you have leaks on destruction)

How did you detect leaks?

SZZ: it's a test example, I had to figure out how to work with lists in MQL, so I made tests.

 
Andrey Pogoreltsev:

And most importantly: developers should just finish generics, make allocators inside and support custom objects. C++ already invented everything for them) And we don't have to invent bicycles.

Write it if it's that simple.

 
Igor Makanu:

how were the leaks detected?

ZS: it's a test example, I had to figure out how to work with lists in MQL, so I made tests.

You create copies of objects in AddValue via new, but you don't free them in destructor, you just clear pointer list.

 
TheXpert:

write if it's that simple.

When I have the time and the need, I will write. And in general, as an example, setting up your own bearing factory is not a good idea if you only need one bearing model)