OOP, templates and macros in mql5, subtleties and uses - page 18

 
fxsaber:
#define  p_func(NAME)  Print(#NAME " = " ,NAME)
//+------------------------------------------------------------------+
void OnStart()
{  int QWERTY=100;
   int zxcvbn=999;
   p_func(QWERTY);   // QWERTY = 100
   p_func(zxcvbn);   // zxcvbn = 999

}
//+------------------------------------------------------------------+

it works!

Thank you!

 

Help me solve this problem. There is a macro and I want to add code inside it to return the result.

#define  FOR3(a,b,c,loop) {for(int i=0; i<a; i++)\
for(int j=0; j<b; j++)\
for(int k=0; k<c; k++)\
{loop;}/*код хочу дописать в этом месте и вернуть результат но эта штука работает только как void*/}

Is it possible to add code in the given place so that I could return the result from the macro? You can add the code; I tried it but if you write return, the effect is not expected. I saw this example

  double Try_helper;
#define  Try(EXPR, MES)               \
   Try_helper = EXPR;                \
   if (Try_helper <= 0.0) {          \
     printf("Error: %s ", MES);      \
     return 4;                         \
   }

here in the thread where return works.

 
Seric29:

Help me solve this problem. There is a macro and I want to add code inside it to return the result.

Is it possible to add code in the given place so that I could return the result from the macro? You can add the code; I tried it but if you write return, the effect is not expected. I saw this example

here in the topic where return works.

Call a function in which you pass a pointer to another function (loop body, for example)

int for3(int a, int b, int c, void(*loop)()) {
        for(int i=0; i<a; i++)
                for(int j=0; j<b; j++)
                        for(int k=0; k<c; k++) 
                                loop();
        return 4;
}
...
void fn() {}
...
if ( for3(1, 2, 3, fn) ) printf("hellow world");
 
Vict:

Call a function in which you pass a pointer to another function (the body of a loop, for example)

I didn't know it was possible to do this, but my goal is to write a complex multi-line macro that will execute many commands, probably a macro can't execute many commands and making it a function is not possible? Sometimes there is a need to extract for example a loop from a function, I was wondering if it's possible, maybe I'm writing nonsense.

 
Seric29:

I didn't know it was possible to do such a thing, but my task is to write a complex multi-line macro, which will execute many commands, probably a macro can't execute many commands and making a function out of it is impossible? Sometimes there is a need to make a loop out of a function, I wonder if it's possible, maybe I'm writing nonsense.

I don't know what the task is and whether you are digging there, but at a glance - another approach:

#define  CHECKCONT_INST(_NAME)                            \
bool _NAME(upindex_t start, upindex_t top)               \
   double lev_k[] = {CHECKCONT_INST_LEVELS};             \
   ...                                                   \
}                                                        \

#define  CHECKCONT_INST_LEVELS
CHECKCONT_INST(check_cont_withoutlev);
#undef  CHECKCONT_INST_LEVELS

#define  CHECKCONT_INST_LEVELS 1.618
CHECKCONT_INST(check_cont_1618);
#undef  CHECKCONT_INST_LEVELS

#define  CHECKCONT_INST_LEVELS 1.618, 2.618
CHECKCONT_INST(check_cont_2618);
#undef  CHECKCONT_INST_LEVELS

check_cont_withoutlev(0, 10);
check_cont_1618(0, 10);
check_cont_2618(0, 10);

This way you can replace any piece of function (not necessarily array filling as it is in my case) and instantiate it as needed.

 
#define  FOR3(a,b,c,code)   \
for(int i=0; i<a; i++)     \
for(int j=0; j<b; j++)     \
for(int k=0; k<c; k++){    \
code;                      \
}

void OnStart(){

   string x="";
   FOR3(2,3,4,x=x+(string)i+","+(string)j+","+(string)k+";\n");   
   Alert(x);
   
}
 
Seric29:

I didn't know it was possible to do such a thing, but my task is to write a complex multi-line macro, which will execute many commands, probably a macro can't execute many commands and making a function out of it is impossible? Sometimes I may need to make a loop out of a function, I wonder if it is possible, maybe I am writing nonsense.

It is possible, but not like this.

 
Dmitry Fedoseev:

You can, but not like this.

This macro will be void, in order to return a result from it you need an expression to be calculated, in other words you need to call a function inside it, I've come to this conclusion.

 
Vict:

Call a function in which you pass a pointer to another function (loop body, for example)

I studied a function pointer, to be honest I never understood what it's for, in fact you can call a function instead of a variable may come to me tightly, a function pointer resembles an overload or polymorphism.

 
Seric29:

I've been studying the function pointer, to be honest I haven't understood what it's for, basically you can call a function instead of a variable maybe I'm getting a hard time, the function pointer is like an overload or polymorphism.

You have some algorithm inside which you can put a sub-algorithm, so to speak, to "tweak" it to solve a particular problem. In essence, it's an analogue of virtual functions. The main thing is you asked for it yourself, and now you don't know why)), you had a framework, inside which you put a loop.