Use pointers. This behaviour is present at least since 2017, good luck if you want it to be fixed.
I created this thread to alert the devs and other programmers coming from C++, this behaviour can generate cryptic errors.
I used the workaround I posted since I was implementing C++'s iterators and pointers made no sense in that context,
it worked but took sometime to understand why such simple code wasn't compiling.
This code reproduces the bug:
There are 2 workarounds for this code, you either remove the const qualifier
from the method foo or create a temporary object before returning it.
Not sure if that alters your original intension, is it?
class C { public: C () {} C (const C &) {} C foo() const { return new C(); } }; int OnInit() { C c; return INIT_SUCCEEDED; }
Not sure if that alters your original intension, is it?
The new operator would return a pointer C*, it would not work as I would have to delete it to prevent memory leaks
which could be solved by returning an SharedPointer but it would come back to the same problem, if a method has
the const qualifier, the compiler does not reconize that:
C foo() const { return C(); } // The workaround. C foo() const { C tmp; return tmp; }
Are equivalent but only the second one works.
An use case for returning copies instead of pointers are C++ iterators, I was able to implement them in MQL5 using
the workaround and the usage got pretty close to the C++ ones, but this bug was puzzling so I created this topic
mainly to alert others that might be having compilation issues on bigger projects.
The new operator would return a pointer C*, it would not work as I would have to delete it to prevent memory leaks
How about this(double meaning..)? Didn't check if it returns the pointer, but seems unlikely
class C { public: C () {} C (const C &) {} C foo() const { return this; } }; int OnInit() { C c; return INIT_SUCCEEDED; }
How about this(double meaning..)? Didn't check if it returns the pointer, but seems unlikely
That works in the context of the example and I indeed used it when overloading the operator+=, but with the operator+ I needed to return a copy to allow
other operations with iterators to work as it would on C++.
The class C example was just the to isolate the bug.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This code reproduces the bug:
There are 2 workarounds for this code, you either remove the const qualifier
from the method foo or create a temporary object before returning it.