Errors, bugs, questions - page 1889

 

Is this a bug or am I missing something? A normal parabolic EA places a stop order at the previous point, in the case of buy with the spread taken into account.
And so the price (Ask) has reached the order price 57781, but the order is not opened.

 
What is the mistake of performing
class CLASS1
{
public:
  int i;  
};

class CLASS2 : public CLASS1 {};

void OnStart()
{
  CLASS1 Object;
  CLASS2* Ptr = dynamic_cast<CLASS2*>(&Object);
  
  Ptr.i = 1;
}
 
fxsaber:
What is the runtime error

if you do this:

class CLASS1
{
public:
  int i;  
};

class CLASS2 : public CLASS1 {};

void OnStart()
{
  CLASS2 Object;
  CLASS1* Ptr = dynamic_cast<CLASS1*>(&Object);
  
  Ptr.i = 1;
}

i.e. conversion to parent, then everything goes correctly, apparently very significant changes in language structure took place and now derived type is unambiguously not an ancestor type.

Checked on C++:

class CLASS1
{
public:
  int i;  
};

class CLASS2 : public CLASS1 {};

void OnStart()
{
  CLASS1* Object = new CLASS1();
  CLASS2* Ptr = (CLASS2 *)&Object;
  
  Ptr->i = 1;
}

Pre-conversion of a parent class object to a derived one is handled correctly, i.e. there is an error in the MQL5 mechanism with type conversion, unless it was not designed that way.

 
fxsaber:
What is the execution error

If you don't check what dynamic_cast returns, there is no point in using it.

 
Konstantin:

if you do this:

i.e. conversion to parent, then everything goes correctly, apparently a very significant change in the language structure has taken place and now the derived type is unambiguously not an ancestor type.

Parental casting has always worked in MQL without dynamic_cast - classically. Perhaps I do not understand the meaning of dynamic_cast. I do not perhaps understand the meaning of the example in the documentation. Somebody could explain.
 
Koldun Zloy:

If you don't check what dynamic_cast returns, there's no point in using it.

Thanks, in my example it returns NULL. Why does this happen?

Could you give a practical example of how to use this trick?


Developers use it only in Graphic.mqh as follows

      curve=dynamic_cast<CCurve*>(m_arr_curves.At(i));

      if(CheckPointer(curve)!=POINTER_DYNAMIC)
         return(false);

At the same time they have definitions of classes/objects like this

class CCurve : public CObject

CObject          *At(const int index) const;

That is, they pass a parent pointer to a child. Well, that's exactly what I do in my example! Where is the snag?

 

A pointer to a parent class can actually contain a pointer to a descendant.

class Class1
{
};

class Class2 : public Class1
{
};


Class1* a = new Class2();

Class2* b = dynamic_cast< Class2* >( a );

// b != NULL
 
Koldun Zloy:

A pointer to a parent class can actually contain a pointer to a descendant.

Thanks, got it!
 
fxsaber:
Thank you, I got it!

I still don't understand why in MQL you can't cast a pointer to a descendant class pointer type, but in C++ it's possible, can you explain if it's a bug in MQL or it should be like that?

 
Konstantin:

is it possible in C++, can you explain if it's a bug in MQL or should it be?

In C++ it is also possible only if a pointer to a base class points to a descendant.
Reason: