Can someone help here ... the operator is overloaded, actually it should work
STest operator++(int) { STest tmp; tmp=this; value+=1; return(tmp); }
-
void operator ++ (void) { value+=1; }
That is operator ++t. - 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++
- 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.
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.
Thx guys.
By the way, is there a possibility to get this done with overloading / cast operator overloading?
STest t; double v=t; // "=" Illegal operation
By the way, is there a possibility to get this done with overloading / cast operator overloading?
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); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Can someone help here ... the operator is overloaded, actually it should work