Questions on OOP in MQL5 - page 48

 
Alexey Navoykov:
Actually it doesn't really matter what language MQL is like, the main thing is that it is supposed to be higher-level than C++, and the code in it should be simpler and more laconic. But in fact it is vice versa. The code in MQL is much more cumbersome and clumsy. You have to crutch many things together.
It reminds me of WinApi by sensation)))
 

anyone strong to show how to port to MQL :

https://ru.wikipedia.org/wiki/Хранитель (design pattern) / C++ non-standard version of the pattern

tried it head-on, something went wrong with the forward descriptions

//+------------------------------------------------------------------+
class Originator
{
public:
   int state;
   Originator() : state(0)    {                             }
   class Memento;
   Memento* getMemento();
   void setState(int s)       { state = s;                  }
   void dumpState()           { printf("State: %i", state); }
   class Memento
   {
   public:
      class Originator;
      int state;
      Originator* org;
      Memento() : state(0)    {                             }
      void restoreState();
   };
};
//+------------------------------------------------------------------+
Originator::Memento* Originator::getMemento()
{
   Originator::Memento* m = new Originator::Memento();
//   m.org = this;
//   m.state = state;
//   m->org = this;
//   m->state = state;
   return m;
}
//+------------------------------------------------------------------+
void Originator::Memento::restoreState()
{
//   org->state = state;
}
//+------------------------------------------------------------------+
void OnStart()
{

}
//+------------------------------------------------------------------+
 
Igor Makanu:

anyone strong to show how to port to MQL :

https://ru.wikipedia.org/wiki/Хранитель (design pattern) / C++ non-standard version of the pattern

tried it head-on, something went wrong with the forward descriptions

My version:

//+------------------------------------------------------------------+
class Originator
  {
public:
   int               state;
                     Originator() : state(0)    {                             }
   class             Memento;
   Memento*          getMemento();
   void              setState(int s)       { state = s;                  }
   void              dumpState()           { printf("State: %i", state); }
   class Memento
     {
   public:
      //class          Originator;
      int            state;
      Originator*    org;
                     Memento() : state(0)    {                             }
      void           restoreState();
     };
  };
//+------------------------------------------------------------------+
Originator::Memento* Originator::getMemento()
  {
   Originator::Memento* m = new Originator::Memento();
   m.org = GetPointer(this);
   m.state = state;
   return m;
  }
//+------------------------------------------------------------------+
void Originator::Memento::restoreState()
  {
   this.state = state;
  }
//+------------------------------------------------------------------+
void OnStart()
  {
   Originator org_obj;
   Originator::Memento* ptr_mem_obj = org_obj.getMemento();
   delete ptr_mem_obj;
  }
//+------------------------------------------------------------------+
 
Denis Kirichenko:

My version:

great! good example!

Thank you!

 
Alexey Navoykov:
The main thing is that it's supposed to be higher-level than C++, and the code in it should be simpler and more laconic. But in fact it's vice versa. The code in MQL is much more cumbersome and clumsy. Many things have to be crutched.

Nah, that's a short joke.) Maybe you had the wrong major in life, maybe you should have gone to a culinary technical school.

 
Alexey Navoykov:
In fact, it doesn't really matter what language MQL looks like. The main thing is that it is supposed to be more high-level than C++, and the code in it should be simpler and more concise. But in fact it is the opposite. The code in MQL is much more cumbersome and clumsy. Many things have to be crutched and manipulated.

The main problem here is the porting itself, not even the porting in general, but how you can get to such an idea.

 

This is what happens when children with immature minds learn STL.

And MQL - its creators just perfectly showed that they, more than anyone else, understand the actual problems of programmers and the challenges of programming. If you don't understand it, maybe you really should think about a culinary technical school?

The main problem in MQL, the main and the only one, is to think through the algorithm that ensures the reliable execution of the strategy. It's not a language problem at all. No language or syntax can provide this by itself. That's why your poking around on the level of syntax is a bright demonstration that you are still at a kindergarten level. That is you haven't even learned how to code, not to get to the stage of developing stable algorithms. But at least you've got... a whole wagonload.

 
Igor Makanu:

excellent! good example!

Thank you!

class Originator
  {
   int               state;
public:
   class Memento;
   Originator() : state(0){}
   Memento*          getMemento()      {return new Memento(&this);}
   void              setState(int s)   {state=s;}
   int               State()           {return state;}
   void              dumpState()       {printf("State: %i", state);}
///////////////////////////////////////////////////////////
   class Memento{
      int            state;
      Originator*    org;
      public:
                        Memento(Originator* mOrg):state(mOrg.State()),org(mOrg){}
         void           restoreState() {org.setState(state);}
   };
///////////////////////////////////////////////////////////
  };

void OnStart()
  {
   Originator org_obj;
   Originator::Memento* ptr_mem_obj = org_obj.getMemento();
   org_obj.setState(6);
   org_obj.dumpState();
   ptr_mem_obj.restoreState();
   org_obj.dumpState();
   delete ptr_mem_obj;
  }

That seems about right

 
Vladimir Simakov:

This seems to be the right way

OK, I'll save it, I needed to test the principle of work, maybe this is what I was looking for - one code for tester and for working EA, which can save its state, and in the tester on the contrary not to waste resources on "extra gestures" - part of such pattern can be covered by #ifdef -ami ;)

 

Pattern single, if anyone needs it of course:

class Alone{
private:
   int state;        
   Alone():state(0){}
public:
   static Alone* Get(){
      static Alone alone;
      return &alone;}
   int State() {return(state);}
   void State(int _state) {state=_state;}
};

void OnStart()
  {
   Test(123);
   Alone* test=Alone::Get();
   Print(test.State());
  }
  
void Test(int x){
   Alone* test=Alone::Get();
   test.State(x);}

I've been craving a good one since beer)))

Getting old I guess))))