mql5中的OOP、模板和宏,细微之处和用途 - 页 22

 
fxsaber:

谢谢你!

但还没有想出如何覆盖宏的方法

下面是代码。

#define  MACROS_PREFIX(A) FAST_##A

bool Connect(string host = "localhost", int port = 6379)
   {
   return(MACROS_PREFIX(connect)(host, port));
   }

   
   
bool FAST_connect(string host = "localhost", int port = 6379)
{
return true;
}

bool connect(string host = "localhost", int port = 6379)
{
return true;
}

它现在工作,但与前缀FAST_ , 我想覆盖MACROS_PREFIX , 但使连接(字符串 主机= "localhost",int 端口= 6379称为

 
Igor Makanu:

我想覆盖MACROS_PREFIX,但调用connect(string host = "localhost", int port = 6379)

#undef  MACROS_PREFIX
#define  MACROS_PREFIX(A) A
 
Igor Makanu:

谢谢你!

但还没有想出如何覆盖宏的方法

下面是代码。

它现在工作,但与前缀FAST_ , 我想覆盖MACROS_PREFIX , 但使连接(字符串 主机= "localhost", int 端口= 6379称为

#define  F_CALL(dPrefix,dFunc) dPrefix##_##dFunc

void Foo()  {Alert("Null");}
void _Foo() {Foo();}
void SomePref_Foo()  {Alert("SomeFunc");}

void OnStart(){
   F_CALL(,Foo)();
   F_CALL(SomePref,Foo)();
}
 

关于交易、自动交易系统和交易策略测试的论坛

mql5中的OOP、模板和宏,技巧和窍门

Vladimir Simakov, 2020.02.18 05:56

#define  F_CALL(dPrefix,dFunc) dPrefix##_##dFunc

 F_CALL(, Foo)();

这很奇怪,它能起作用,但它不能。

F_CALL()
 
fxsaber:

这很奇怪,它能起作用,但它不能。

我的宏有两个参数,但你传了一个,所以编译器发誓。

 

谢谢...但我仍然没有弄明白....科学来自地狱!!!!

我无法用语言来解释,我会用纸条语言试试。

这段代码按计划工作。

bool USE_SAVE_MODE = true;                      // проверка пинга
bool ping() { return(true); }                   // заглушка 
bool connect(string h,int p) { return(true);}   // заглушка 
//+------------------------------------------------------------------+
bool ExPing()
   {
   if(USE_SAVE_MODE)
      {
      Print("safe mode on");
      return(ping());
      }
   else
      {
      Print("safe mode off");
      return(true);
      }

   }
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return ExPing() && connect(host, port);
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
      USE_SAVE_MODE = false; // отключили проверку пинга
      Connect();
   }
//+------------------------------------------------------------------+

但我想在MQL中做一个宏替换,尽量减少代码文本本身,所以我需要。

#define   USE_FAST                               // тут нужно что-нибудь сделать!
bool connect(string h,int p)      {    }        // код 1
bool fast_connect(string h,int p) {    }        // код 2
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return connect(host, port);                  // вот тут нужен префикс и будет или connect(host, port);  или fast_connect(host, port); 
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
   }
//+------------------------------------------------------------------+
 
Vladimir Simakov:

我的宏有两个参数,而你传递了一个,这就是为什么编译器在战斗的原因

它不起作用。

   F_CALL(,)
 
fxsaber:

它不起作用。

当然是这样。你有它的扩展在_
 
Igor Makanu:

谢谢...但我仍然没有弄明白....科学来自地狱!!!!

我无法用语言来解释,我会用纸条语言试试。

这段代码按计划工作。

但我想在MQL中做一个宏替换,以尽量减少代码文本本身,所以我需要它。

#define   USE_FAST                               // тут нужно что-нибудь сделать!
#ifdef  USE_FAST
   #define  CONNECT fast_connect
#else
   #define  CONNECT connect
#endif
bool connect(string h,int p)      {    }        // код 1
bool fast_connect(string h,int p) {    }        // код 2
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return CONNECT(host, port);                  // вот тут нужен префикс и будет или connect(host, port);  или fast_connect(host, port); 
   }
//+------------------------------------------------------------------+
void OnStart()
   {
      Connect(...);
   }
//+------------------------------------------------------------------+

但是!!!。

CONNECT宏不能被#undef USE_FAST所覆盖。

 

所以我可以做我想做的事。

#define   USE_FAST
bool connect(string h,int p)      { Print("On");   return true; }
bool fast_connect(string h,int p) { Print("Off");   return true;}
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   #ifdef  USE_FAST
   return connect(host, port);
   #else return fast_connect(host, port);
   #endif 
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
   }
//+------------------------------------------------------------------+


对昨天使用魔咒建议的选项感兴趣 #defaine abracadabra#_

UPD。

弗拉基米尔-西马科夫 好的,我试着去弄清楚

UPD。

欢呼吧!它成功了!(С)

#define   USE_FAST
#ifdef  USE_FAST
   #define  CONNECT fast_connect
#else
   #define  CONNECT connect
#endif

bool connect(string h,int p)      { Print("On");   return true; }
bool fast_connect(string h,int p) { Print("Off");   return true;}
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return CONNECT(host, port);
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
   }
//+------------------------------------------------------------------+


谢谢你!它如愿以偿地起飞了。


PZY: 我如何将__MQL5__和__MQL4__宏添加到这个天罡地煞中,但fast_xxx()函数根本就不会出现在MQL4代码中--从字面上看,NEVER

原因: