The only situation for B to be assigned with A is if it has a constructor or overload of the assignment operator accepting A as follows:
class B : public Base{ public: B(){} B(A&){} void operator=(A&){} };
Another way to think of that assignment is what the compiler normally does, since it is initializing the object, it passes the value from assignment
to a constructor:
B b = a;
// It technically converts the previous line to a constructor call.
B b(a);
But in that specific case, it is ignoring the value in the assignment and just calling B's default constructor.
class Base{}; class A : public Base{}; class B : public Base{}; int main() { A a; B b = a; return 0; }It will show the error:
clang++-7 -pthread -std=c++17 -o main main.cpp main.cpp:9:5: error: no viable conversion from 'A' to 'B' B b = a; ^ ~ main.cpp:5:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'A' to 'const B &' for 1st argument class B : public Base{}; ^ main.cpp:5:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'A' to 'B &&' for 1st argument class B : public Base{}; ^ 1 error generated. exit status 1
At first I thought MQL5 was generating an implicit copy constructor, which would not be great either, but no, it calls the default constructor
and ignores the value assigned as if nothing was being passed.
That's exactly the point, if I didn't write a constructor in B to accept A, it means I don't want the code "B b = a" to compile,
this is why C++ shows this error. Errors like this are useful when the code base gets large.
If you remove the base class, MQL is able to to perform this check, so it should show an error in this case also.
This bug can really caught me off guard when I was refactoring some code, at one point I mixed a generic list iterator with an
array iterator, the indicator compiled fine but it just didn't work, thank God I am using git to manage the source code so it
took only minutes to isolate issue.
Don't be discoraged by it, recommend you try the generics too, it still an awesome language and it is the only platform that allowed me to
write pretty complex stuff. I am hoping it'll have other C++'s features like returning references and exceptions. Just have to be aware
of some quirks of the compiler.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use