Questions on OOP in MQL5 - page 78

 

Forum on trading, automated trading systems and strategy testing

FAQ on MQL5

Vladimir Simakov, 2020.06.10 19:06

#define  LOG(dText) CLog::Ptr().Log((string)__LINE__,__FUNCSIG__,dText)

class CLog{
   int cHndl;
   CLog():cHndl(FileOpen(MQLInfoString(MQL_PROGRAM_NAME)+_Symbol+(string)(int)TimeCurrent()+".log",FILE_TXT|FILE_WRITE)){}
  ~CLog() {FileClose(cHndl);}
public:
   static CLog* Ptr() {static CLog _log; return &_log;}
   void Log(string line,string sig,string text){
      string _text=StringFormat("Line: %s. Signature: %s. %s",line,sig,text);
      PrintFormat(_text);
      FileWrite(cHndl,_text);}
};


Have you ever made the object not directly accessible instead of the highlighted one, like below?

#define  LOG(dText) CLog::_log.Log((string)__LINE__,__FUNCSIG__,dText)

class CLog{
   int cHndl;
   CLog():cHndl(FileOpen(MQLInfoString(MQL_PROGRAM_NAME)+_Symbol+(string)(int)TimeCurrent()+".log",FILE_TXT|FILE_WRITE)){}
  ~CLog() {FileClose(cHndl);}
public:
   static CLog _log;  
   void Log(string line,string sig,string text){
      string _text=StringFormat("Line: %s. Signature: %s. %s",line,sig,text);
      PrintFormat(_text);
      FileWrite(cHndl,_text);}
};

static CLog CLog::_log;


This design is not very useful, since it cannot be widely applied.

int f() { return(1); }

if (LOG(LOG(f()) -1) || LOG(LOG(f()) + 1))
  LOG(f());


I've done the profiling of necessary calls in the same way.

 
Vladimir Simakov:

Well done! That's right! And this?

You have to admit that it's a bit more complicated.)

What if a user opens a file in some unknown editor and starts a new test? He/she will search for an error in the code, fix it, look through the log, but the log doesn't change...

And what if there is a big crash and terminal crashes? The most interesting thing is that it will not be saved in the log.

 
Dmitry Fedoseev:

What if the user opens the file in some unknown editor and starts a new test? He will look for error in the code, fix it, look in the log, but the log does not change...

And what if there is a big crash and terminal crashes? The most interesting thing is that it won't save in the log.

And who prevents you from doing FileFlush? This is a prototype as an example, then everyone makes his own luck, and they usually ask for a fee for completed solutions)))) So open it in anything))) In this implementation, each launch has its own file)))

UPD: I gave an example where OOP makes life easier, and then write as you like, no one cancelled the saying about hand-jobbery)))).
 
Vladimir Simakov:

Who prevents you from doing FileFlush? This is a prototype as an example, then everyone makes his own luck, and for completed solutions they usually ask for money)))) So open it in anything))) In this implementation, each launch has its own file))))

UPD: I've shown an example, when OOP makes life easier, and further, write as you want, no one cancelled saying about handwork)))).

Ouch, didn't notice that the file name is new every time. ThenaddFileFlush() and with beer will pull.

 
Dmitry Fedoseev:

Ouch, didn't notice that the file name is new every time. Thenadd FileFlush() and beer will do the trick.

As far as I remember, FileFlush in MQL has been empty for a long time. It's useless to call it.
 
Alexey Navoykov:
As far as I remember, FileFlush in MQL has been empty for a long time. It's useless to call it.

HZ. You can't tell without tests.

 
WithoutFileFlush and without FileClose to disk reset. Closed terminal via task manager.
 
Alexey Navoykov:
As far as I remember, FileFlush in MQL has been an empty file for a long time. It is useless to call it.

not empty, in MQL4 I know for a fact that without FileFlush you cannot make offline charts online (offline...online...great and mighty is the Russian language! ))) )

until you reset the new data to the history, there will be nothing new on the offline chart

another thing is that the terminal automatically resets and closes the file if the user has not done so.... in the tester at the beginning of the year, even after closing the file it is impossible to open....


i checked just in case in MQL5 - write one line every 3 sec. and print the counter in the log. if the setting is false, then the file will be empty after opening with notepad. if true, then the file content can be read at any time

i.e. FileFlush works as declared

#property script_show_inputs
#include <Files\FileTxt.mqh>

input bool UseFileFlush = true;
//+------------------------------------------------------------------+
void OnStart()
{
   CFileTxt f;
   f.Open("tstFileFlush.txt", FILE_WRITE | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_COMMON);
   int cnt = 0;
   while(!IsStopped())
   {
      f.WriteString(string(++cnt) + " : QWERTY\n");
      if(UseFileFlush) f.Flush();
      printf("cnt = %i", cnt);
      Sleep(3000);
   }
}
//+------------------------------------------------------------------+
 

for the subject

how does casting a class pointer to a parent type work?

or is it done at compile time and a table of function (method) calls will be matched?

 
Igor Makanu:

for the subject

how does casting a class pointer to a parent type work?

or will it be done at compile time and a table of function (method) calls will be matched?

In this case, it is not expensive at all. It is costly (per dereferencing) to call virtual methods.

Reason: