Download MetaTrader 5

Inheritance in MQL

To add comments, please log in or register
sagh0901
8
sagh0901  

Hello,

 

I am learner and beginner in MQL. I am trying to achieve inheritance possiblity:

Below is situation where i am finding diffculties.

 

template <typename T>

class A

{


protected:


   virtual T Func()

   {

     Print("from Class A");

     return NULL;

   }


public:

   A(){Execute();}

   void Execute()

   {

      Func(); 

   } 


}; 

template <typename T>

class B : public  A<T>

{

public:

   B(){}

 protected:


   T Func()

   {

      Print("from Class B");

      return NULL;

   } 


}; 

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---

   A<int>* objA= new A<int>();

   //objA.Execute();

   B<float>* objB= new B<float>();

   //objB.Execute();

   delete objA;

   delete objB;

  }


When calling execute, i create instance.

A* objA= new A();

 objA.Execute(); // expectation is to run Func in class A internally

 when i create instance of B

B* objB= new B();

objB.Execute(): // expectation is to run Func in class B internally

 But when the function is called internally within constructor, derived Func in B is never called. 

but in mql it Execute is calling Func in class A in both scenarios. Am i doing it wrongly or is there any other way to achieve this functionality. My main intention is to have one entry point, and multiple derived classes with specific intentions in Func methods. But its not working. Someone direct me properly here. Thanks. 

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
honest_knave
Moderator
2318
honest_knave  
sagh0901:

objA.Execute(); // expectation is to run Func in class A internally

objB.Execute(): // expectation is to run Func in class B internally

But when the function is called internally within constructor, derived Func in B is never called. 

but in mql it Execute is calling Func in class A in both scenarios. Am i doing it wrongly or is there any other way to achieve this functionality. My main intention is to have one entry point, and multiple derived classes with specific intentions in Func methods. But its not working. Someone direct me properly here. Thanks. 

Works as expected for me:

 

whroeder1
16102
whroeder1  
sagh0901:

A* objA= new A();  objA.Execute(); // expectation is to run Func in class A internally

B* objB= new B();  objB.Execute(): // expectation is to run Func in class B internally

but in mql it Execute is calling Func in class A in both scenarios.

  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Wrong. Your code, as posted, does exactly what you expect it to do.
    2017.01.31 12:16:51.463 testscr AUDJPY,M1: uninit reason 0
    2017.01.31 12:16:51.463 testscr AUDJPY,M1: from Class B
    2017.01.31 12:16:51.463 testscr AUDJPY,M1: from Class A
    2017.01.31 12:16:51.463 testscr AUDJPY,M1: initialized
To add comments, please log in or register