instanceOf / polymorphism method call

 

Hey guys,

I am currently trying to create a trading panel in MQL5. As I didn't like the standard controls, I decided to create my own based on them.

I created:

- GuiControl

   - GuiContainer

       - GuiPanel

    - GuiButton

     [...]

Basically GuiControl is the base class where all others are derived from.

A GuiContainer has a list of GuiControls.

I would not like to be able to invoke a method that all these elements have, but can override (method is virtual). However, it always calls the method in GuiControl.

For example, for alle elements in a GuiContainer, I would like to call THEIR specific draw method:

for(int i = 0; i < getNumberOfControls(); i++) {

     getControlAtIndex(i).draw(); //In Java, if my control was a panel, this would call GuiPanel.draw(). However, in mql5 it always seems to call GuiControl.draw()

}


Alternatively is it possible to get the class of an object? Something like

if(control instanceOf GuiPanel.class)


Thanks in Advance,

Daniel

 
Hello Chron


You wrote
Chr0n:
...
     However, in mql5 it always seems to call GuiControl.draw()
...

Try this code, it works.
class CFoo
  {
public:
   virtual void      WhoIam() { Print("CFoo"); }
  };

class CBar : public CFoo
  {
public:
   virtual void      WhoIam() { Print("CBar"); }
  };

void OnStart()
  {
   CFoo *foo=new CBar();
   foo.WhoIam();
   delete foo;
  }
Result:
2012.07.16 13:28:46    s1 (EURUSD,H1)    CBar


Reason: