Errors, bugs, questions - page 1890

 
fxsaber:
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:

class CLASS1
{
public:
  int i;  
};

class CLASS2 : public CLASS1 {};

int main() {
  CLASS1 _object;
  CLASS2 *_ptr = (CLASS2*)&_object;  
  _ptr->i = 1;

   return 0;
}

works in C++, which is what I wrote above. And here is the same code (considering MQL syntax):

class CLASS1
{
public:
  int i;  
};

class CLASS2 : public CLASS1 {};

int OnInit() {
  CLASS1 _object;
  CLASS2 *_ptr = dynamic_cast<CLASS2 *>(&_object);
  
  _ptr.i = 1;

   return 0;
}
no longer works because _ptr gets NULL
Which begs the question, is it a bug in MQL and will it be fixed or will it stay like this?
 
Renat Fatkhullin:

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 ))
 
Konstantin:

I don't know what you mean by that, but this code:

Well, try to understand it. Start by making dynamic_cast work in the pluses. If you figure it out on your own, you'll be much better off.
 
Renat Fatkhullin:

That's right, you can't drive from the bottom up, only from the top down.

What do you mean, you can't be cast from a base class to a descendant?
 
Комбинатор:
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.

 
Renat Fatkhullin:

That's right, you can't drive from the bottom up, only from the top down. This is for safety's sake.

If we bring the pointer from top to bottom, i.e. to the parent, and then pass the pointer somewhere else in the scope, are the fields of the descendant available there?
 
Renat Fatkhullin:

Yes.

Renate, do you know what dynamic_cast is?
 
Комбинатор:
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
}

Reason: