Questions on OOP in MQL5 - page 39

 

Now it is not strange why there is not enough Volchanskiy here, tough guy Insider preview or whatever he uses))


I downloaded win 10home from the off site 3 months ago, did not even have time to configure the basic settings, the startup Options icon does not open, yes ***********

 
Koldun Zloy:

Here is an example:

Tried to fully apply your example to my task, but again the same rake - I want to end up with the "record required" flag when creating a new descendant from a base class:

//+------------------------------------------------------------------+
class CStrategy
{
private:
   int f_save_required;
protected:
   int set;
   void              SaveRequiredFlags (const bool flag)                      { f_save_required++;  }
   int               SaveRequiredFlags (void) const                           { return(f_save_required); }
public:
                     CStrategy():f_save_required(0)                           {  Print(__FUNCSIG__,"save = ",f_save_required);  }
                     CStrategy(int setting):set(setting),f_save_required(0)   {  Print(__FUNCSIG__,"save = ",f_save_required);  }
                    ~CStrategy(){}

};
//+------------------------------------------------------------------+
class A: public CStrategy
{
public:
   void              SaveRequired (void)                                      { SaveRequiredFlags(true);                         }
   int               getSaveRequired (void) const                             { return(SaveRequiredFlags());                     }
                     A(int st_setting) : CStrategy(st_setting){}
                    ~A(){}
};
//+------------------------------------------------------------------+
class B: public CStrategy
{
public:
   void              SaveRequired(void)                                       { SaveRequiredFlags(true);                         }
   int               getSaveRequired (void) const                             { return(SaveRequiredFlags());                     }
                     B(int st_setting) : CStrategy(st_setting){}
                    ~B(){}
};
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   A *a = new A(1);
   B *b = new B(2);
   a.SaveRequired();
   a.SaveRequired();
   a.SaveRequired();
   a.SaveRequired();
   b.SaveRequired();
   Print(a.getSaveRequired());
   Print(b.getSaveRequired());
//---
   return(INIT_SUCCEEDED);
  }

2019.09.14 16:07:26.856 Builder_v1.00 (EURUSD,H1) CStrategy::CStrategy(int)save = 0

2019.09.14 16:07:26.856 Builder_v1.00 (EURUSD,H1) CStrategy::CStrategy(int)save = 0

2019.09.14 16:07:26.856 Builder_v1.00 (EURUSD,H1) 4

2019.09.14 16:07:26.856 Builder_v1.00 (EURUSD,H1) 1

for now everything that has worked out:

1. not declaring interfaces was not a problem at all

2. I generated a lot of setters and getters but was unable to get away from static variables


I tried to rewrite the code again:

//+------------------------------------------------------------------+
class CStrategy
{
private:
   int f_save_required;
protected:
   int set;
   void              SaveRequiredFlags (const bool flag)                      { f_save_required++;  }
   int               SaveRequiredFlags (void) const                           { return(f_save_required); }
public:
virtual  void        SaveRequired (void)                                      { SaveRequiredFlags(true);                         }
virtual  int         getSaveRequired (void) const                             { return(SaveRequiredFlags());                     }
                     CStrategy():f_save_required(0)                           {  Print(__FUNCSIG__,"save = ",f_save_required);  }
                     CStrategy(int setting):set(setting),f_save_required(0)   {  Print(__FUNCSIG__,"save = ",f_save_required);  }
                    ~CStrategy(){}

};
//+------------------------------------------------------------------+
class A: public CStrategy
{
public:
                     A(int st_setting) : CStrategy(st_setting){}
                    ~A(){}
};
//+------------------------------------------------------------------+
class B: public CStrategy
{
public:
                     B(int st_setting) : CStrategy(st_setting){}
                    ~B(){}
};
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   CStrategy *a = new A(1);
   CStrategy *b = new B(2);
   
   a.SaveRequired();
   a.SaveRequired();
   a.SaveRequired();
   a.SaveRequired();
   b.SaveRequired();
   Print(a.getSaveRequired());
   Print(b.getSaveRequired());
//---
   return(INIT_SUCCEEDED);
  }

the result is the same as in the first version, the only thing is that I got away from interfaces

 
What's the goal, what's to be gained?
 
Dmitry Fedoseev:
What's the goal, what do you want to get?

everything works, but the goal is to gain knowledge ))))

how i did it:

- there is a base class in which all calculations and it stores TC settings, from it we inherit descendants that work by their TC

- I think it is reasonable to save the file into one file by passing the handle of the open file to all descendants; in this case, the save() method appears in the base class again

- saving should be performed when an event/flag occurs - you don't know which descendant initiates it and when, but it is faster to save dozens of structures to a new file than to figure it out

- why not make one class and an array of settings? - I have to declare one or two methods as virtual and add them to each TS.

- the problem: i'm trying to get away from static variables in the hope that there's a jedi OOP trick i don't know





SZY: const modifiers are a good thing, I don't regret the time spent - useful, thanks again for the discussion!

 

If you have to write data from all objects of this class into one file, which is opened once, then you can't do it without a static variable. And why get rid of it, if it best suits the task? Of course, you can declare a global variable too.

What if every object would open, write and close a file? Then it would be sufficient to have a file name in every object... But in this case, too, it's better to use a static variable, because the value of the variable is always the same everywhere.

Or I don't understand anything)

 
Dmitry Fedoseev:

If you have to write data from all objects of this class into one file, which is opened once, then you can't do it without a static variable. And why get rid of it, if it best suits the task? Of course, you can declare a global variable too.

What if every object would open, write and close a file? Then it would be sufficient to have a file name in every object... But in this case, too, it's better to use a static variable, because the value of the variable is always the same everywhere.

Or maybe I've got it wrong)

In the file, which is opened once, we write data in it and immediately close, so as not to lose data if the terminal hangs up, recording from once in a minute to once an hour - there's no sense to keep the file open for so long

let's do it in code, that's how it is now:

// это один файл, его не трогаю, тут только конструктор и 3 метода из интерфейсов, чтобы дать стратегиям тик, чтобы закрыть их и вот чтобы записать все ТС в файл

//____________________________________________________________________
void EA::OnTick(double &profit[])
{
      for(int i = 0; i < StrategyCount; i++) profit[i] = Strategy[i].Strategy();  // запустили зоопарк стратегий и тут нам интересно дать им волю и узнать их профит, если ТС завершилась       
                                                                                  // она самостоятельно запустит "пустую стратегию", но профит будет возвращать первоначальный
      if(CStrategy::GetSaveRequired()) Save();                                    // вот тут узнаем состояние флага требуется ли запись состояния любой из ТС
}
//____________________________________________________________________
//это структура которая умеет открыть и записать файл, пример от fxsaber
struct FILE
{  const int handle;

  FILE( const string FileName, const int Flags ) : handle(::FileOpen(FileName, Flags)) {}
 ~FILE( void ) { if (this.handle != INVALID_HANDLE) ::FileClose(this.handle);  Print(__FUNCTION__);} 
};

//____________________________________________________________________
// метод для записи в файл состояния всех ТС, но проще записать все ТС один раз в файл, чем искать одну ТС..... в общем считаем, что скорость доступа к диску намного меньше чем работа с памятью
// используем структуру FILE - ну это просто удобно, объявили и забыли, она сама закроет файл когда уйдет из области видимости
void  EA::Save(void)
{  FILE f(DEF_PREFIX_FILE_NAME + IntegerToString(m_magic)+".bin",FILE_WRITE|FILE_BIN);
   for(int i = 0; i < StrategyCount; i++)
      Strategy[i].SaveSelfStatus(f.handle); 

}





// это основной файл (инклудник) в котором работаю, тут ТС 

//____________________________________________________________________
здесь опять все кратко и лаконично, но сделаю вывод в лог ошибки если возникнут при записи, сделаю именно здесь, т.к. только тут все модификации 
void CStrategy::SaveSelfStatus(const int handle)
{  FileWriteStruct(handle,m_setting);
   FileWriteStruct(handle,m_order_info);

}

 
Dmitry Fedoseev:

Or maybe I didn't understand anything.

I don't think I understood anything, I read the article diagonally last month, came back to it again this month, but need to test to see feasibility

Hubr:Singleton or static class?

Singleton (Одиночка) или статический класс?
Singleton (Одиночка) или статический класс?
  • habr.com
Статья будет полезна в первую очередь разработчикам, которые теряются на собеседованиях когда слышат вопрос «Назовите основные отличия синглтона от статического класса, и когда следует использовать один, а когда другой?». И безусловно будет полезна для тех разработчиков, которые при слове «паттерн» впадают в уныние или просят прекратить...
 
Igor Makanu:

- Problem: I'm trying to get away from the static variable in the hope that there's a Jedi OOP trick I don't know

Why is this a problem? You don't need to get away from a static variable if you need it.

I still don't understand what the write need flag depends on.

In my example it is set if there have been changes.

How do you want it set?

 
Koldun Zloy:

Why is this a problem? You don't have to walk away from a static variable if you need one.

then the problem is solved! - That's how it was originally done.

ZS: already started experimenting with static structure... I see only problems with initialization, then with code readability, I tried variants flag.saveRequired, then flagsave.Required, then I got into overload operator =, anyway, nonsense all this. It is much easier and readable code in the end if you make it via private static variable and add setter and getter to it, but as they say: we are not looking for easy ways.... all the best to the authors of obscure videos...

 
Fast235:

Now it is not strange why there is not enough Volchanskiy here, tough guy Insider preview or whatever he uses))

I downloaded win 10home from the off site 3 months ago, did not even have time to configure the basic settings, the startup Options icon does not open, yes ***********

Just busy and forum is time consuming, I don't recommend to installInsider preview, now Vinda installs new build once a week. Better to download the stable build from MS site. It's my childish curiositystill playing in one place :)

Reason: