Calling an overridden function from the overriding function?

 

Hi

Is is possible to call a function in an inherited class from the function that overrides it? This is useful to avoid duplicating code when several classes inherit from one base class. A simple example is below.

Thanks, Jellybean

//=================================================================================================
// Generic strategy object
class CStrategy
{
private:
   string               m_Symbol;
   ENUM_TIMEFRAMES      m_Period;

protected:
   double               m_Score;

public:
                        CStrategy() {};
                       ~CStrategy() {};

   virtual int          OnInit( string Sym, ENUM_TIMEFRAMES Per );
};

int CStrategy::OnInit( string Sym, ENUM_TIMEFRAMES Per )
{
   m_Symbol = Sym;
   m_Period = Per;
   
   return(0);
}

//=================================================================================================
// Specific strategy object
// Inherits from class CStrategy
class CStrategy_MA1 : public CStrategy
{
private:

public:
                        CStrategy_MA1() {};
                       ~CStrategy_MA1() {};

   int                  OnInit( string Sym, ENUM_TIMEFRAMES Per );
};

//-------------------------------------------------------------------------------------------------
int CStrategy_MA1::OnInit( string Sym, ENUM_TIMEFRAMES Per )
{
   // call the base class OnInit( Sym, Per ); from here???

   // more initialisation stuff here
   // .
   // .
   // .
   
   return(0);
}

//=================================================================================================
 
int CStrategy_MA1::OnInit( string Sym, ENUM_TIMEFRAMES Per )
{
   CStrategy::OnInit( Sym, Per );

   // more initialisation stuff here
   // .
   // .
   // .
   
   return(0);
}

 

See also Scope Resolution Operation:

#property script_show_inputs
#import "kernel32.dll"
   int GetLastError(void);
#import
 
class CCheckContext
  {
   int         m_id;
public:
               CCheckContext() { m_id=1234; }
protected:
   int         GetLastError() { return(m_id); }
  };
class CCheckContext2 : public CCheckContext
  {
   int         m_id2;
public:
               CCheckContext2() { m_id2=5678; }
   void        Print();
protected:
   int         GetLastError() { return(m_id2); }
  };
void CCheckContext2::Print()
  {
   ::Print("Terminal GetLastError",::GetLastError());
   ::Print("kernel32 GetLastError",kernel32::GetLastError());
   ::Print("parent GetLastError",CCheckContext::GetLastError());
   ::Print("our GetLastError",GetLastError());
  }  
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   CCheckContext2 test;
   test.Print();
  }

 

Thank you both Stringo and Rosh. I know I had read it somewhere, but I couldn't find it afterwards.

Jellybean


Reason: