Questions on OOP in MQL5 - page 37

 
Koldun Zloy:

This is all understandable. It is not clear whySetSaveRequired() andGetSaveRequired() are non-static, but write to a static variable.

I don't understand it, that's why I asked for help. I rarely use modifiers, operating more as an intuitive method, but i always want to find the best solution

Let me try it in the code, it's like this

//+------------------------------------------------------------------+
interface IStrategy
{  double      Strategy();
   bool        Finalize(); };
//+------------------------------------------------------------------+
class CStrategy: public IStrategy
{
public:
   bool              GetSaveRequired() { return(CStrategy::f_save_required); CStrategy::f_save_required = false;  }
protected:
   static bool       f_save_required;
   double            ProfitStrategy;
   void              SetSaveRequired() { CStrategy::f_save_required = true;                                       }
   void              RunStrategy();
   virtual void      NextStepStrategy();
   void              EndStrategy();
};

//____________________________________________________________________
void CStrategy::RunStrategy(void)
{  ProfitStrategy = 0.0;
   SetSaveRequired();
//...........    
}
//____________________________________________________________________
void CStrategy::EndStrategy(string func)
{ //...........    
   SetSaveRequired();
}
//____________________________________________________________________
#define CLASS(NAME) class NAME : public CStrategy{\
public:\
   NAME(SSettingsForOrder &set)  { m_setting = set;}\
   ~NAME()                       { delete m_order; }\
   virtual void NextStepStrategy();}
//+------------------------------------------------------------------+
CLASS(CStrategy_XXX);
CLASS(CStrategy_YYY);
CLASS(CStrategy_ZZZ);

void CStrategy_XXX::NextStepStrategy(void) { SetSaveRequired();   //...........     }
void CStrategy_YYY::NextStepStrategy(void) { SetSaveRequired();   //...........     }
void CStrategy_ZZZ::NextStepStrategy(void) { SetSaveRequired();   //...........     }


publicGetSaveRequired() is used in a class to manage all this zoo of strategies

with this pattern I write only methodNextStepStrategy - only in it changes for TC, everything else is the same, so in the base class CStrategy I collect all the methods and all the fields - they are not much

ZS: with my back I feel that not ace my code, but it will definitely work and will not create duplicates of variablef_save_required;

 
Igor Makanu:

This is what I don't understand, that's why I asked for help. I rarely use modifiers, I use them more intuitively, but as always I want the best solution

Let me try it in the code, it's like this


publicGetSaveRequired() is used in a class to manage all this zoo of strategies

with this pattern I write only methodNextStepStrategy - only in it changes for TC, everything else is the same, and therefore in the base class CStrategy I collect all the methods and all the fields - they are not much

ZS: with my back I feel that not ace my code, but it will definitely work and will not create duplicates of variablef_save_required;

I would remove these functions altogether.

All the functions that do not change the structure, make them constants.

All non-constant functions should set write flag.

I would also add the Save( int fileHandle ) function. This function should write the structure and reset the flag.

You can make a static function to get the state of the flag.

if( CStrategy::GetSaveRequired() )
{
   int fileHandle = FileOpen( ... );
   for( int i = 0; i < 10; i++ )
   {
      strategies[i].Save( fileHandle );
   }
   FileClose( fileHandle );
}
 
Koldun Zloy:

I would remove these functions altogether.

Make all functions that do not change the structure constant.

All non-constant functions should set write flag.

I'm telling you, I'm having trouble with modifiers in general.


SZY: in the meantime what is going on in general in the web in programming discussion - here was an article about const on hobber last month, the meaning - this const is not necessary, look assembler code is no different without const - the compiler will take it all away ((

 
Igor Makanu:

I'm telling you, I'm having trouble with modifiers in general.

I googled and read, something went wrong again...

class A {
private:
  int x;
public:
  void f(int v) const { Print("const");                  }
  void f(const int v) { x = v; Print("(const) x = v ");  }
//  void f(int v)       { x = v; Print("x = v ");          } // 'f' - function already defined and has body     
};

A a;
//+------------------------------------------------------------------+
void OnStart()
{ 
   int y       = 1;
   const int z = 2;
   a.f(3);
   a.f(y);
   a.f(z); 
 }
//+------------------------------------------------------------------+

2019.09.13 21:10:54.316 tst (EURUSD,H1) x = v

2019.09.13 21:10:54.316 tst (EURUSD,H1) x = v

2019.09.13 21:10:54.316 tst (EURUSD,H1) x = v

expected to getPrint("const") at least once;
 
Igor Makanu:

Googled, read, something went wrong again...

2019.09.13 21:10:54.316 tst (EURUSD,H1) x = v

2019.09.13 21:10:54.316 tst (EURUSD,H1) x = v

2019.09.13 21:10:54.316 tst (EURUSD,H1) x = v

expected to getPrint("const") at least once;
class A {
private:
  int x;
public:
  void f(int v) const { Print("const");                  }
  void f(const int v) { x = v; Print("(const) x = v ");  }
//  void f(int v)       { x = v; Print("x = v ");          } // 'f' - function already defined and has body     
};

A a;
const A b;
//+------------------------------------------------------------------+
void OnStart()
{ 
   int y       = 1;
   const int z = 2;
   a.f(3);
   a.f(y);
   a.f(z);
   b.f(3);
   b.f(y);
   b.f(z);
 }
This is an overload for a constant class.
 
Vladimir Simakov:
This is an overload for a constant class.

Hmmm... right, I spent nearly half an hour reading both the hub and the forums, but I couldn't get the hang of it.

Thanks! Now I've got it

There is still the suspicion of such an example:

class A {
private:
  int x;
public:
  void f(int v) const { Print("const");                  }
  const void f(int v) { x = v; Print("(const) x = v ");  }

};

A a;
const A b;
//+------------------------------------------------------------------+
void OnStart()
{ 
   int y       = 1;
   const int z = 2;
   a.f(3);
   a.f(y);
   a.f(z);
   b.f(3);
   b.f(y);
   b.f(z);
 }

2019.09.13 22:04:34.295 tst (EURUSD,H1) (const) x = v

2019.09.13 22:04:34.295 tst (EURUSD,H1) (const) x = v

2019.09.13 22:04:34.295 tst (EURUSD,H1) (const) x = v

2019.09.13 22:04:34.295 tst (EURUSD,H1) const

2019.09.13 22:04:34.295 tst (EURUSD,H1) const

2019.09.13 22:04:34.295 tst (EURUSD,H1) const


gave the same values, but the point is quite different? - Then you need the help of the hall again.
 
I'm sitting at a lecture on OOP at the moment. Maybe I'll share something useful with you )).
I can download the current lecture. So far, of course, the trivial stuff.
 
Nikolai Semko:
I'm attending a lecture on OOP at the moment. (Maybe I'll share what I'll learn))
I may download the current lecture. So far, of course, the trivial stuff.

Well, your lecture is most likely in gibberish, while here you can hardly learn anything in Russian, you've gone around the Internet for the third time... the overall situation is similar to the one here on the forum, there are no more than 10 real programmers and the rest are geeks

Ok, the overall situation has cleared up with const, I've worked with the rest of the modifiers, so I've had enough theory... I need to apply it, but I feel like a student )))

 
Igor Makanu:

Well, your lecture is most likely in gibberish, while here you can hardly learn anything in Russian, you've gone around the Internet for the third time... the overall situation is grim like here on the forum, real programmers - no more than 10 people on the resource, the rest are geeks

Ok, the overall situation has cleared up with const, I've worked with the rest of the modifiers, so I've got a lot of theory ... I feel like a student ))).

Yes, I too have a similar situation. Learning is not difficult, it's harder to retrain, break old habits. I still can't kick many bad habits of procedural programming.

 
Igor Makanu:

I'd like an example, I'm telling you, I'm not good with modifiers at all.


SZY: moreover in general the horror what is going on in the web in programming discussion - here was an article about const on hobber last month, the meaning - yes, this const is not necessary, look assembler code does not differ that without const - the compiler will remove everything (((

You may simply forget about constants and statics for now. On interfaces too.

And when everything is ready, then look and mark something as a statics and something as a constant. And you can forget about interfaces altogether.