Errors, bugs, questions - page 1890
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
What is the execution error
That's right, you can't drive from the bottom up, only from the top down. This is for safety's sake.
You can't compare with C++ - anything can be reduced to anything there.
In C++ it also works only if a pointer to a base class points to its descendant.
I don't know what you mean by that, but this code:
That's right, you can't drive from the bottom up, only from the top down. This is for safety's sake.
You can't compare with C++ - anything can be reduced to anything there.
Got it now, thanks for the clarification ))
I don't know what you mean by that, but this code:
That's right, you can't drive from the bottom up, only from the top down.
You mean you can't cast from a base class to a descendant?
Yes, in case the base class does not have an actually constructed descendant.
That's right, you can't drive from the bottom up, only from the top down. This is for safety's sake.
Yes.
Renat, do you know what dynamic_cast is?
Of course.
Look at the discussed piece of MQL5 code. A base instance is created, and then it heroically tries to convert via dynamiccast to descendant in violation of security. Well, it's a bummer, of course.
The same example is directly discussed and explained in the documentation. The dynamic cast at runtime is triggered only after the security system and permissibility of conversions have been checked. Every MQL5 object has all meta-information for checking rights at runtime. This is not empty C++.
Dynamic type conversion using the dynamic_cast operator
It is possible to dynamically cast types using the dynamic_cast operator, which can only be applied to class pointers. In this case, the check of correctness of types is performed at the moment of program execution. It means that when using dynamic_cast operator the compiler doesn't check the data type used for the conversion. If conversion of a pointer to a data type, which is not the actual object type, is performed, the result will be NULL.
dynamic_cast<type-id> ( expression )
The type-id parameter in angle brackets must be a pointer to a previously defined class type . Theexpressionoperand type (as opposed to C++) can be anything except void.
Example:
class CBar { };
class CFoo :public CBar { };
voidOnStart( )
{
CBar bar;
//--- dynamic cast of pointer type *bar to pointer *foo is allowed
CFoo *foo =dynamic_cast<CFoo*>(&bar);//--- no critical error of execution
Print(foo);// foo=NULL
//--- explicitly trying to cast a reference of the Bar object to a Foo object is prohibited
foo=(CFoo *)&bar;// a critical execution error will occur
Print(foo);// this line will not be executed
}