简体   繁体   中英

What is the correct way to define MQL4 “#import of static class methods”?

What I'm trying to achieve is define classes (using MQL4 ) in separate files and use the methods from those classes in the main code. Essentially importing static class member functions.

 class example{                                            // ____ in example.mq4
   public:
   static void myfunction(void) export { .. do something .. }
 }

 class example{                                            // ____ in example.mqh
   public:
   static void myfunction(void);
 }

 #include <example.mqh>                                    // ____ in main.mq4:
 #import "example.ex4"
     void example::myfunction(void);
 #import

Results in a compile error when using the function as follows:

void OnInit(){
   example::myfunction();
}

compiler error:

myfunction: function must have a body

(note example.mq4 is compiled to example.ex4 and can be imported ok)

"new"-MQL4 syntax is evolving

for the indicated purpose,

the class definition syntax shall be enough, upon instantiation of a class, its public methods are possible to be invoked on instance-objects.

Compile-time syntax:

Exporting a function without it's class -inheritance ( taking together the whole class container ) does not fit the OOP concept. This is clearly visible in the OnInit() call, where your code attempts to call a function , which is in fact a class-based object- method at a moment, where there has not yet been instantiated any object , on which the method ought to be performed anObjectINSTANCE.aClassOrInstanceMETHOD() .

So, just #include

class example{                                            // ____ in example.mqh

   public:
   static void myfunction() { .. do something; }

  }

// ----------------------------                          // ____ in main.mq4
#property strict            // "new"-MQL4

#include <example.mqh>      // include

example anObject;           // create a globally visible instance

// ----------------------------
void OnInit(){
     anObject.myfunction(); // request <anObject> to perform <myfunction> method on self

     return( EMPTY );       // "new"-MQL4 insists on return even on void fun()
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM