Questions on OOP in MQL5 - page 19

 
Vladimir Simakov:
I have an assumption that there will be no lot, for example 0.15. At least I have not heard about it. If I do, I will change it.

I know it's unlikely, but you always want the perfect solution ))))

Ok, it's not the biggest problem, but I've created a problem for myself... I wanted a perfect OOP, but with no unnecessary calls and everything was optimal

I want to have 5 instances of the class, but I need to initialize the class fields once - they contain terminal environment, which will not change during the execution of the program.

string         Language;

If I declare static string Language; - then there will be an error unresolved static variable 'A::Language'

here is an example

//+------------------------------------------------------------------+
class A
  {
public:
   string         Language;
                     A() { Print(__FUNCTION__); Language = TerminalInfoString(TERMINAL_LANGUAGE); }

  };
//+------------------------------------------------------------------+
class B:public A
  {
public:
                     B() {Print(__FUNCTION__,Language); }
  };
//+------------------------------------------------------------------+
B *arrB[5];
//+------------------------------------------------------------------+
int OnInit()
  {
   for(int i=0; i<5; i++)
      arrB[i] = new B;
   return(INIT_SUCCEEDED);
  }

I will initialize string Language 5 times in this form - it is wrong and it will not compile with static modifiers

and there's no need to have 5 copies of the Language variable! - tried in class B to declare an instance of the class - same thing 5 Language variables have

2019.08.29 12:44:58.384 tst__ EURUSD,M15: initialized

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:57.989 tst__ EURUSD,M30: uninit reason 3

 
Igor Makanu:

I know it's unlikely, but you always want the perfect solution ))))

Ok, it's not the biggest problem, but I've created a problem for myself... I wanted a perfect OOP, but with no unnecessary calls and everything was optimal

I want to have 5 instances of the class, but I need to initialize the class fields once - they contain terminal environment, which will not change during the execution of the program.

If I declare static string Language; - then there will be an error unresolved static variable 'A::Language'

here is an example

I will initialize string Language 5 times in this form - it's not correct, and it doesn't compile with static modifiers

2019.08.29 12:44:58.384 tst__ EURUSD,M15: initialized

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:57.989 tst__ EURUSD,M30: uninit reason 3

class Test
  {
private:
   static const string Language;
public:
                     Test(void){}
                    ~Test(void){}
  };
static const string Test::Language=::TerminalInfoString(TERMINAL_LANGUAGE);
 
Andrey Barinov:

OK, that's an option

But I want to wrap it all in one class - in my example I have class B and I want 5 instances of B, but I don't want to initializeLanguage 5 timesand therefore I don't want to have aLanguagevariablein each instance

How do I get rid of extraLanguage ?

 
Igor Makanu:

OK, that's an option

But I want to wrap it all in one class - in my example I have class B and I want 5 instances of B, but I don't want to initialize Language 5 times and therefore I don't want to have aLanguage variablein each instance

How do I get rid of extraLanguage?

Inherit. This way it will only be initialized once.

 
Igor Makanu:

I know it's unlikely, but you always want the perfect solution ))))

Well, it's not the biggest problem, but I've created a problem for myself... I wanted a perfect OOP, but with no unnecessary calls and everything was optimal

I want to have 5 instances of the class, but I need to initialize the class fields once - they contain terminal environment, which will not change during the execution of the program.

If I declare static string Language; - then there will be an error unresolved static variable 'A::Language'

here is an example

I will initialize string Language 5 times in this form - it is wrong and it will not compile with static modifiers

and there's no need to have 5 copies of the Language variable! - tried in class B to declare an instance of the class - same thing 5 Language variables have

2019.08.29 12:44:58.384 tst__ EURUSD,M15: initialized

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:58.384 tst__ EURUSD,M15: B::BRussian

2019.08.29 12:44:58.384 tst__ EURUSD,M15: A::A

2019.08.29 12:44:57.989 tst__ EURUSD,M30: uninit reason 3

I'm on my phone right now. Remind me tonight and I'll show you how to solve it.
 
Andrey Barinov:

Inherit. In this form, it will only be initialised once.

Works for me

//+------------------------------------------------------------------+
string MyTerminalInfoString()
  {
   Print(__FUNCTION__);
   return("ru");
  }
//+------------------------------------------------------------------+
class CLang
  {
protected:
   static const string Language;
public:
                     CLang(void) {Print(__FUNCTION__);}
                    ~CLang(void) {}
  };
//+------------------------------------------------------------------+
static const string CLang::Language=MyTerminalInfoString();
//+------------------------------------------------------------------+
class B:public CLang
  {
public:
                     B() {Print(__FUNCTION__,Language); }
  };
//+------------------------------------------------------------------+
B *arrB[5];
//+------------------------------------------------------------------+
int OnInit()
  {
   for(int i=0; i<5; i++)
      arrB[i] = new B;
   return(INIT_SUCCEEDED);
  }

2019.08.29 13:24:19.690 tst__ EURUSD,M15: initialized

2019.08.29 13:24:19.690 tst__ EURUSD,M15: B::Bru

2019.08.29 13:24:19.690 tst__ EURUSD,M15: CLang::CLang

2019.08.29 13:24:19.690 tst__ EURUSD,M15: B::Bru

2019.08.29 13:24:19.690 tst__ EURUSD,M15: CLang::CLang

2019.08.29 13:24:19.690 tst__ EURUSD,M15: B::Bru

2019.08.29 13:24:19.690 tst__ EURUSD,M15: CLang::CLang

2019.08.29 13:24:19.690 tst__ EURUSD,M15: B::Bru

2019.08.29 13:24:19.690 tst__ EURUSD,M15: CLang::CLang

2019.08.29 13:24:19.690 tst__ EURUSD,M15: B::Bru

2019.08.29 13:24:19.690 tst__ EURUSD,M15: CLang::CLang

2019.08.29 13:24:19.690 tst__ EURUSD,M15: MyTerminalInfoString


at least myTerminalInfoString() is called once


So I kinda got a Singleton

Thank you!

 
Vladimir Simakov:
I'm on my phone right now. Remind me in the evening and I'll show you how to solve it.

OK, if it's not too much trouble.

and the Singleton theme is fairly new to me, there is room for creativity - examples would

 
Igor Makanu:

OK, if it's not too much trouble.

and the Singleton theme is fairly new to me, there is room for creativity - examples would

So do you want a singleton, or a general field for objects?
 
Vladimir Simakov:
So, do you need a common field for the objects?

it's hard to say what i want ))))

Let me try to put it into words:

- I want a class that is initialized by the constructor and this class will not have any methods; the class purpose is to open an order and then close and/or reopen several more orders according to a script; the script type is an enumeration

- I will use an array of instances of the class - at least 5

- I don't want to open the order without calling the terminal environment every time. Therefore, I want to use SYMBOL_VOLUME_MAX, Symbol, SYMBOL_VOLUME_MIN and the number of digits in SYMBOL_VOLUME_STEP as constant values in the class

- I want optimal use of variables, i.e. I don't want to duplicate the constants in the previous paragraph.


That's about how I see it all, but in general, the problem is now reduced to the correct structure of the class itself, as it turned out already have to inherit, so that SYMBOL_VOLUME_MAX , Symbol,SYMBOL_VOLUME_MIN ... to have one copy, I think I will still stumble on the error output, I will use something like this from my PrintLastError.mqhhttps://www.mql5.com/ru/code/24829#

 
A static variable, even without inheritance, will be the same for all instances.
Reason: