Using OnInit() in a mqh (include) file

 
I would like to involve OnInit() in a .mqh file so that OnInit() will be executed both in the main .ex4 file and in the .mqh file. Is this possible? One must then override OnInit() in the .mqh file in some way.
 
Write an OnInitIncludefileName() and call it in OnInit()
 
Jan Flodin:
I would like to involve OnInit() in a .mqh file so that OnInit() will be executed both in the main .ex4 file and in the .mqh file. Is this possible? One must then override OnInit() in the .mqh file in some way.

The #include directive literally copies and pastes all text from the mqh file to that exact place in the file prior to compilation. In other words, it's not like other languages which makes you define the module namespace. So if you include a mqh that has defined an OnInit with a file that already has one then you will get errors because now you are redefining a function with the same signature.  

 
nicholi shen:

The #include directive literally copies and pastes all text from the mqh file to that exact place in the file prior to compilation. In other words, it's not like other languages which makes you define the module namespace. So if you include a mqh that has defined an OnInit with a file that already has one then you will get errors because now you are redefining a function with the same signature.  

yep, forgot to mention that I already got the error and therefor was thinking of overriding OnInit() in the .mqh file. I am sure that this is possible but the suggestion from whroeder seems more straighforwarded so I will stick to that. 

Thanks a lot anyway for the explanation :-)

 
Jan Flodin:

yep, forgot to mention that I already got the error and therefor was thinking of overriding OnInit() in the .mqh file. I am sure that this is possible but the suggestion from whroeder seems more straighforwarded so I will stick to that. 

Thanks a lot anyway for the explanation :-)

You can't override a function like that... you could overload it with a different signature, but remember -- #include grabs the text from the other file and pastes it as the exact location that you are calling it from 

So

//other.mqh

int OnInit(){ return INIT_FAILED; }
double global_double = 1.21;
//program.mq4

int OnInit() {return INIT_SUCCEEDED;}

#include "other.mqh"

This is what the compiler sees

//program.mq4

int OnInit() {return INIT_SUCCEEDED;}

int OnInit(){ return INIT_FAILED; }
double global_double = 1.21;

Which is an error because you can't redefine a function with the same signature. 

 
Why doing that ? I don't get it.
 

I accidentally deleted my last post instead of editing... You can make a wrapper similar to a decorator in Python like this.

init_decorator.mqh

int OnInit()
{
   Print("Enter decorator");
   int main_file_init = __OnInit();
   Print("Exit decorator");
   return main_file_init;
}
#ifndef OnInit 
#define OnInit __OnInit
#endif 

main.mq4

#include "init_decorator.mqh"
int OnInit()
{
   Print("I am the OnInit func in the main file.");
   return(INIT_SUCCEEDED);
}

output:

Generic EURUSD,H1: Enter decorator
Generic EURUSD,H1: I am the OnInit func in the main file.
Generic EURUSD,H1: Exit decorator
Generic EURUSD,H1: initialized
 
nicholish en:

I accidentally deleted my last post instead of editing... You can make a wrapper similar to a decorator in Python like this.

init_decorator.mqh

main.mq4

output:

This is fantastic!
 
nicholish en:

I accidentally deleted my last post instead of editing... You can make a wrapper similar to a decorator in Python like this.

init_decorator.mqh

main.mq4

output:

... but...

Error in main: __OnInit Function already defined and has body 

 
William Roeder:
Write an OnInitIncludefileName() and call it in OnInit()
This works best for me, thanx!
Reason: