How to overload the "Print" function for print a custom object

 

I created a custom struct 

struct MyStruct
{
int a;
int b;
}

I want to print a object that is created from this struct

void _Print()
{
Print("i = ", i, "b = ", b);
}


To do it easier I want to overload the Print function and just do like this:


MyStruct obj;

obj.a = 1;
obj.b = 2;

Print(obj);

As a result, I want to see this

Output:

a = 1, b = 2

How to overload the Print function for print a custom object?

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You don't overload the Print function. Create methods inside the object and call them.

struct MyStruct
{
int a;
int b;

string as_string(){ return StringFormat("a = %i, b = %i", a,b); }
void   Print(){     ::Print(as_string());                       }
} // MyStruct
//////////////////////////
obj.Print(); // It prints itself.
Reason: