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

 

tried it, but it didn't work...

how do I make a macro to override the default parameter in a callable function in an inluded file?

I have the first file, which is an "plugin":

class CObj{
public:
   CObj(string host="127.0.0.1"){}
};

CObj obj = new CObj();

I want to connect this inluder, but with a possibility to substitute the text"127.0.0.1" with macros (if necessary)

i.e. I need some define here:

#define  defaulthost "192.168.1.10"
#include "mylib.mqh"

but so that if there is no this define, then host="127.0.0.1" will work if there is adefaulthost definethen use it

 
Igor Makanu:

if this define is missing, then host="127.0.0.1" will work ifdefaulthost is present, then use it

#ifdef, #else, #endif.

 
fxsaber:

#ifdef, #else, #endif.

I know that - those are the words I'm looking for

I can't figure out what to put in the brackets.

 
Igor Makanu:

I know that's what I'm looking for.

I can't figure out what to put in the brackets.

//#define defaulthost "192.168.0.1"

class CObj{
public:
  #ifdef  defaulthost 
   CObj(string host=defaulthost){}
  #else // defaulthost 
   CObj(string host="127.0.0.1"){}
  #endif // defaulthost 
};

CObj obj = new CObj();
 
fxsaber:
#ifndef  DEFINE_HOST
   #define  DEFINE_HOST "127.0.0.1"
#endif

class CObj{
public:
   CObj(DEFINE_HOST){}
};

Same thing, but more readable.

 
Vladimir Simakov:

Same thing, but more readable.

#ifdef  EXTERNAL_DEFINE_HOST
   #define  DEFINE_HOST EXTERNAL_DEFINE_HOST
#else
   #define  DEFINE_HOST "127.0.0.1"
#endif

class CObj{
public:
   CObj(DEFINE_HOST){}
};

And this is even more flexible.

 

Thank you! It works!

ZS: got confused between 2 files - needed to get a workable macro substitution in one file first, then spread out between files ((

 

There's a great library for MT4 by a good man that solves many of the quad's problems in a very original way. Written in fxsabera style, it doesn't make shit sense, but it works! I think you will find it interesting.

Good luck

dingmaotu/mql4-lib
dingmaotu/mql4-lib
  • dingmaotu
  • github.com
MQL4/5 programming language provided by MetaQuotes is a very limited version of C++, and its standard library is a clone of the (ugly) MFC, both of which I am very uncomfortable with. Most MQL4 programs have not adapted to the MQL5 (Object Oriented) style yet, let alone reuable and elegant component based design and programming. mql4-lib is a...
 

is there any way to add a small prefix to the function name using a macro prefix,

i.e. i have this code in the "inluder":

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

I want to define a macro in the "master file" so that the above code becomes this:

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

i.e. i'm looking for some way to change the name of a group of functions

 
Igor Makanu:

is there any way to prefix the function name with a small macro prefix

#define  MACROS_PREFIX(A) FAST_##A

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