Thank you for sharing.
Do you have a practical real world example where this can be useful ?
The usual case for an up cast is when you are processing a collection of objects (array of pointers to the base type) but need to call a method that is not virtual in the base class, or want to call it for those objects that support it.
There are only three options:
- Refactor the paradigm (expensive.)
- Add a second virtual function to the base class (may not be possible) that just calls the first and override that method in a derived class. This complicates the base class, for something only a derived class uses:
class Text{ virtual Print(); virtual PrintFancy(){ Print(); } // ? What formatting does text have? } class Html{ virtual Print(); // Drops formatting virtual PrintFancy(); // Keeps formatting } Printer(Text& t){ t.PrintFancy(); // ? what does it mean for text to print Fancy?
- Try to up cast the pointer and if it succeeds call the method. Base class (Text) and any classes derived from Base (WrappedText) are unaffected. Only the class with the new method (and subclasses from there are aware of the change.)
class Text{ virtual Print(); } class WrappedText : public Text{ virtual Print(); // Word wrapping }; // But no fancy printing here class Html : public Text{ virtual Print(); // Drops formatting virtual PrintFancy(); // Keeps formatting } Printer(Text& t){ Html h = Dynamic_Cast<h&> t; if(h) h.PrintFancy(); else t.Print();
In C++ you up cast it in a try/catch block or use a dynamic_cast. dynamic_cast - Wikipedia, the free encyclopedia
Mql4/5 doesn't have either.
The usual case for an up cast is when you are processing a collection of objects (array of pointers to the base type) but need to call a method that is not virtual in the base class, or want to call it for those objects that support it.
There are only two options:
- Add a second virtual function to the base class (my not be possible) that just calls the first and override that method in a derived class. This complicates the base class, for something only a derived class uses:
- Try to up cast the pointer and if it succeeds call the method. Base class (Text) and any classes derived from Base (WrappedText) are unaffected. Only the class with the new method (and subclasses from there are aware of the change.)
In C++ you up cast it in a try/catch block or use a dynamic_cast. dynamic_cast - Wikipedia, the free encyclopedia
Mql4/5 doesn't have either.
Also I make the macro calls protected in the actual objects as they aren't supposed to be called.
class Polymorphic{ public: template<typename D> bool dynamic_cast(D*& dp) const{ ... } class Object : public Polymorphic_NC{ // Noncopyable protected: POLYMORPHIC(Polymorphic_NC); : } class Derived : public Object{ // Noncopyable protected: POLYMORPHIC(Object); public: void DeriveOnly(){ ...} : } Object* obj = ...; Derived* derv; if(object.dynamic_cast(derv) ) derv.DeriveOnly();Also note that I have not provided const versions because the compiler (880 and earlier) can't cast constants
derive* d = (const derive*)b; 'const' - unexpected token testscr.mq4 67 16
Operator dynamic_cast will be added
What will be the advantage in comparison to the current casting?
This will be probably implemented
B* my_b = dynamic_cast<B*>(my_a);
and this is how it works currently:
B* my_b = my_a;
Did I miss anything, or is it just a plan to complicate things?
Currently, if my_a is not a B the assignment results in the termination of the script.
Last line of Typecasting - MQL4 Documentation
Currently, if my_a is not a B the assignment results in the termination of the script.
Last line of Typecasting - MQL4 Documentation
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
See the attached file for more examples. (Russian post динамическое приведение - MQL4 форум)