繁体   English   中英

定义MQL4“静态类方法的#import”的正确方法是什么?

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

我想要实现的是在单独的文件中定义类(使用MQL4 ),并在主代码中使用这些类中的方法。 本质上是导入静态类成员函数。

 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

使用以下函数时会导致编译错误:

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

编译器错误:

myfunction: function must have a body

(注意example.mq4被编译为example.ex4 ,可以导入)

“新” -MQL4语法正在发展

用于指定目的

类定义语法应足够,在实例化一个类时,可以在实例对象上调用其public方法。

编译时语法:

导出没有class继承的函数(将整个class 容器合并在一起)不适合OOP概念。 这在OnInit()调用中清晰可见,您的代码尝试调用一个函数 ,该函数实际上是当前基于类的对象method ,而尚未实例化该method应在其上的任何object要执行anObjectINSTANCE.aClassOrInstanceMETHOD()

因此,只需#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()
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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