Questions on OOP in MQL5 - page 95

 
Maxim Kuznetsov:

the GA is more of a "buzz word" thing. How can Gray's code help convergence? Just theoretically...

you might as well just randomly shuffle

i'm not saying anything, i don't believe everything written on the internet for a long time, i try to check everything

here's a working conversion code to Gray's code and back

//+------------------------------------------------------------------+
uint IntToGrey(const uint value) // https://e-maxx.ru/algo/gray_code
  {  return(value ^ (value >> 1)); }
//+------------------------------------------------------------------+
uint GreyToInt(const uint value) // https://e-maxx.ru/algo/gray_code
  {
   uint n = 0;
   for(uint g = value; g != 0; g >>= 1)
     {
      n ^= g;
     }
   return(n);
  }
//+------------------------------------------------------------------+
 
And how would this affect the genetic algorithm? The genetic algorithm should not respond to values if it is purely genetic. If some additional averaging or gradient descent is used, it may affect it. But in this case it is better to solve the problem in a direct way - to filter the parameter through the array and shuffle the array. The value of the optimized parameter from the properties window is an index of the array element, and the value from the array will be used in the Expert Advisor itself. Then the optimizer can be tricked. But only if convergence occurs due to averaging or gradient descent. But if it is a pure genetic algorithm, I can't imagine what good it will do. What's the point? Where do the legs grow from?
 

there is this code:

class A
{
private:
   const int _a;
public:
   A(const int a): _a(a) { Print(_a); }
};
//+------------------------------------------------------------------+
class B
{
private:
   const A a;
public:
   B(const int b): a(b) {}
};
//+------------------------------------------------------------------+
void OnStart()
{
   B b(3);// 3
}
//+------------------------------------------------------------------+

everything is OK, but I need an array of A objects, i.e. I need it like this:

class B
{
private:
   const A a[2];
public:
   B(const int b): a[0](b),a[1](b) {}  //'[' - member(s) initialization expected
                                       // 'a' - explicit initializer for arrays not allowed
};

is there any way to call a constructor for an array of objects without using pointers?

 
Igor Makanu:

there is this code:

everything is OK, but I need an array of A objects, i.e. I need it like this:

is there any way to call a constructor for an array of objects without using pointers?

No. Only through crutches.
 

Is it possible for this code:

//+------------------------------------------------------------------+
struct LIST;
struct MyStruct
{
   double            d_data;
   int               i_data;
   MyStruct() {}
   MyStruct(const int i_v, const double d_v): d_data(d_v), i_data(i_v) {}
   LIST              operator+(MyStruct &value)
   {
      LIST result;
      ArrayResize(result.arr, 2, 10);
      result.arr[0] = this;
      result.arr[1] = value;
      return(result);
   }
   MyStruct          operator*(const double mult)
   {
      MyStruct result = this;
      result.d_data *= mult;
      return(result);
   }
};
//+------------------------------------------------------------------+
struct LIST
{
   MyStruct          arr[];
   LIST              operator+(MyStruct &value)
   {
      int last = ArraySize(this.arr);
      ArrayResize(this.arr, last + 1, 10);
      this.arr[last] = value;
      return(this);
   }
};
//+------------------------------------------------------------------+
void OnStart()
{
   MyStruct a(1, 1.11), b(2, 2.22), c(3, 3.33);
   LIST x = a + b + c * 2;
   ArrayPrint(x.arr);
//    [d_data] [i_data]
//[0]  1.11000        1
//[1]  2.22000        2
//[2]  6.66000        3
}
//+------------------------------------------------------------------+

for LIST to make a template / template ?

It didn't work the first time (((.

 
Igor Makanu:

Is it possible for this code:

for LIST to make a template / template ?

it did not work the first time ((.

this. remove

UPD: hmm... you've got it all figured out))))

UPD: get the yum))))

//+------------------------------------------------------------------+
template<typename T>
struct LIST
{
   T          arr[];
   LIST<T>              operator+(T &value)
   {
      int last = ArraySize(arr);
      ArrayResize(arr, last + 1, 10);
      arr[last] = value;
      return(this);
   }
};
//+------------------------------------------------------------------+
struct MyStruct
{
   double            d_data;
   int               i_data;
   MyStruct() {}
   MyStruct(const int i_v, const double d_v): d_data(d_v), i_data(i_v) {}
   LIST<MyStruct>              operator+(MyStruct &value)
   {
      LIST<MyStruct> result;
      ArrayResize(result.arr, 2, 10);
      result.arr[0] = this;
      result.arr[1] = value;
      return(result);
   }
   MyStruct          operator*(const double mult)
   {
      MyStruct result = this;
      result.d_data *= mult;
      return(result);
   }
};
//+------------------------------------------------------------------+
void OnStart()
{
   MyStruct a(1, 1.11), b(2, 2.22), c(3, 3.33);
   LIST<MyStruct> x = a + b + c * 2;
   ArrayPrint(x.arr);
//    [d_data] [i_data]
//[0]  1.11000        1
//[1]  2.22000        2
//[2]  6.66000        3
}
 
Vladimir Simakov:

this. remove

UPD: hmmm... you've got it all figured out))))

UPD: catch the yum-yum)))

Thanks! - it works!

the code is minimalistic, it works - i want an array of structures, lists are not always convenient, everything works, it is clear that non-standard situations using this code can be simulated, but the code a dozen lines

 

why this code does not give a compiler error when accessing someone else's private field:

class A
{
private:
   int _value;
public:
  void set(const int value)      { _value = value;    }
  int  get()                     { return(_value);    }
  void set(A &a, const int value){ a._value = value;  }
};

void OnStart()
{
  A x,y;
  x.set(10);
  y.set(20);
  Print("x = ",x.get());   // 10
  Print("y = ",y.get());   // 20
  y.set(x,777);
  Print("x = ",x.get());   // 777
  Print("y = ",y.get());   // 20
}
//+------------------------------------------------------------------+
 
is its own private field
 
Andrei Trukhanovich:
this is its own private field

OK, thanks!

But if a derived class in a parent class changes private fields in the same way, is it "legal" ?

i.e. in a similar way:

class B: private A
{
        A a;
        void setA(A &obj, const int value) { a.set(obj,value); }
}
....
A x;
B y;
y.(x.55)
Reason: