++/-- Illegal operation use on operator overloading ... Why?

To add comments, please log in or register
Doerk Hilger
1220
Doerk Hilger  

Can someone help here ... the operator is overloaded, actually it should work 

//+------------------------------------------------------------------+
//|                                                     PlusPlus.mq5 |
//+------------------------------------------------------------------+

struct STest
   {
         double value;

         double ToDouble()               { return value; }
         //--- Assignment operators
         void operator =  (STest &dec)   { value=dec.ToDouble(); }
         void operator =  (double v)     { value=v; }

         //--- Logical operators
         bool operator == (STest &dec)   { return (value==dec.value); }
         bool operator == (double v)     { return (value==v); }      
         bool operator != (STest &dec)   { return (value!=dec.value); }
         bool operator != (double v)     { return (value!=v); }
         bool operator >= (STest &dec)   { return (value>=dec.value); }
         bool operator >= (double v)     { return (value>=v); }
         bool operator <= (STest &dec)   { return (value<=dec.value); }
         bool operator <= (double v)     { return (value<=v); }
         bool operator >  (STest &dec)   { return (value>dec.value); }
         bool operator >  (double v)     { return (value>v); }
         bool operator <  (STest &dec)   { return (value<dec.value); }
         bool operator <  (double v)     { return (value<v); }
   
         //--- Arithmetic & binary operators
         void operator -= (STest &dec)   { value-=dec.value; }
         void operator -= (double v)     { value-=v; }
         void operator += (STest &dec)   { value+=dec.value; }
         void operator += (double v)     { value+=v; }
         void operator *= (STest &dec)   { value*=dec.value; }
         void operator *= (double v)     { value*=v; }
         void operator /= (STest &dec)   { value/=dec.value; }
         void operator /= (double v)     { value/=v; }
         void operator ++ (void)         { value+=1; }
         void operator -- (void)         { value-=1; }      
   };


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   STest t;
   t=1;
   t++;        // Illegal operation use ... why?
   
//---
   return(INIT_FAILED);
  }
Alain Verleyen
38626
Alain Verleyen  
Doerk Hilger:

Can someone help here ... the operator is overloaded, actually it should work 

Because you didn't define the overload correctly. You need to return an unchanged STest value.
         STest operator++(int)          { STest tmp; tmp=this; value+=1; return(tmp); }
William Roeder
19422
William Roeder  
  1.          void operator ++ (void)         { value+=1; }
    That is operator ++t.

  2. Alain gave you t++. Alternatively implement postfix using prefix:
             STest operator++(int)          { STest tmp=this; ++this; return tmp; }
    That is why you should prefer ++x over x++

Alain Verleyen
38626
Alain Verleyen  
William Roeder:
  1. That is operator ++t.

Yes but incorrectly defined as it can't be assigned. I don't think it's possible to define it correctly with a struct as a pointer should be returned.

William Roeder
19422
William Roeder  
Alain Verleyen: Yes but incorrectly defined as it can't be assigned. I don't think it's possible to define it correctly with a struct as a pointer should be returned.

Agreed. If it was a class, I'd have a dozen additional comments as const correctness, and returning pointer to self for chaining, etc. But it's not, so I didn't.

Doerk Hilger
1220
Doerk Hilger  

Thx guys.

Doerk Hilger
1220
Doerk Hilger  

By the way, is there a possibility to get this done with overloading / cast operator overloading?

STest t;
double v=t;     // "=" Illegal operation
Alain Verleyen
38626
Alain Verleyen  
Doerk Hilger:

By the way, is there a possibility to get this done with overloading / cast operator overloading?

I don't think it's possible currently in mql.
William Roeder
19422
William Roeder  
That would require operator double(). Not listed at: MQL5 Reference → Language Basics → Functions → Operation Overloading
nicholi shen
2383
nicholi shen  
Alain Verleyen:
Because you didn't define the overload correctly. You need to return an unchanged STest value.

This is only partially correct. You should not return a value from a self mutating operator. As such, the operator should be define as follows:

struct STest {
   double value;
   STest():value(0.0){}
   STest(double n) {
      this.value = n;
   }
   void operator ++ (int) { 
      this.value += 1.0; 
   }
};
//+------------------------------------------------------------------+
void OnStart() {
   STest t = 1;
   t++;   
   Print(t.value);
}
To add comments, please log in or register