Questions on OOP in MQL5 - page 84

 
Pavel Verveyko:
Is it possible to declare a static array in a public class section and initialise it in the constructor? (as below) (or only element by element?)

you can

or maybe you are looking for a static array initialisation after all?

class A
{
public:
   static bool Mass[5];
               A() { ArrayPrint(Mass); }
};

static bool A::Mass[5] = { false, true, false, true, true };
//+------------------------------------------------------------------+
void OnStart()
{
   A a;
}
//+------------------------------------------------------------------+
 
Pavel Verveyko:
can a static array be declared in the public class section and initialised in the constructor? (as below) (or only element by element?)

It goes something like this:

class A{
   protected:
      static int x[];
      int my[];
   public:
      A(){
         ArrayCopy(my,x);
      }   
};

int A::x[]={1,2,3,4,5};

A a;
In general, this poverty of OOP is striking against its brilliance. Why can't we initialize variables and arrays as usual when declaring them? I don't doubt, theorists will now move a lot of theories proving me an idiot, but still...
 
Sergey Dzyublik:

You can initialize a local array and perform ArrayCopy to the appropriate array field:

This was also a thought, it can be placed in global scope outside of any class as well.
It's even easier, but I wanted it to be OOP))

 
Dmitry Fedoseev:

It goes something like this:

In general, this poverty of OOP is striking against the background of its brilliance. Why cannot we initialize variables and arrays as usual when declaring them? I don't doubt, theorists will now move a lot of theories proving that I'm an idiot, but still...

I have the exact same thought)) every time I write using OOP.

 
Pavel Verveyko:

This was also a thought, it can be placed in the global scope outside of any class.
It's even easier, but I wanted it to be OOP))

class CTest{
public:
   int a[4];
   CTest(){
      static int _a[]={1,2,3,4};
      ArrayCopy(a,_a);
   }
};
 
Igor Makanu:

you can

or maybe you are looking for static array initialization after all?

interesting option.

is calling a "method with constructor"?

 
Pavel Verveyko:

interesting option.

is this a "method with constructor" call?

no

that's how you initialize statics.

you don't have to initialize it, this is how the code will look like:

class A
{
public:
   static bool Mass[5];
               A() { ArrayPrint(Mass); }
};

static bool A::Mass[5];
//+------------------------------------------------------------------+
void OnStart()
{
   A a;
}
//+------------------------------------------------------------------+

this is not a method call but the full name of the field of class A::Mass[5];

but if you delete the line, there is a compiler error - statics need to allocate memory before the class instance is created - this is what the yellow line does
 
Igor Makanu:

no

this is how the initialisation of statics is written

you don't have to initialize it, that's how the code will look like:

thank you, it turns out that this line(static bool A::Mass[5];) cannot be transferred to constructor in any way?

 
Pavel Verveyko:

or is this the only way statics are initialised in a case like this?

post above supplemented

yes, statics should be described separately,@fxsaber helped me to understand ithttps://www.mql5.com/ru/forum/325418/page4#comment_16116740

you can initialize static classes in MQL5 too.


Pavel Verveyko:

i.e. this line(static bool A::Mass[5];) cannot be transferred to the constructor?

No, it's a static class field. You need to allocate memory in advance, not at the time of creation of the class instance

and you will use it further as an usual array

If the question is about initialization in the form of a single string, then Sergei's version is what you are looking for, and you just don't need statics.

 
Igor Makanu:

post above supplemented

yes, statics should be described separately,@fxsaber helped me to understand ithttps://www.mql5.com/ru/forum/325418/page4#comment_16116740

you can initialize static classes in MQL as well.

Thanks, now I've tried it and realized that it's possible to access an array that way.

int OnInit()
{
   ArrayPrint(A::Mass);

   return(INIT_SUCCEEDED);
}

To be honest we have OOP, but I haven't seen it in Help. It's a pity it doesn't describe such nuances.

thanks a lot, everyone who responded!

Reason: