Classes, call method from parent class that has the same name from child class

 

Hey y'all

I'm facing a rather frustrating challenge. Consider the following:

  • CAdmin is a parent class.
  • CNotify inherets CAdmin, therefore, is a child from CAdmin.
  • Both classes have a function called Function(void).
  • CAdmin declaration of Function(void) is virtual.
  • CNotify declaration of Function(void) is not virtual.

Given the following code:

CNotify notify;

notify.Function(); // Prints from CNotify
notify.CAdmin::Function(); // Error, want to call the method with name Function but in CAdmin, which is inhereted into CNotify

And the following declaration of both classes:

class CAdmin
{
// ---

// === Public ===

   public:

// --- Functions ---
                    
      CAdmin(void);
      ~CAdmin(void);
     
      virtual void Function() { Print("CAdmin"); }
      
// ---      
};

class CNotify : public CAdmin
{
// ---

// === Public ===

   public:

// --- Functions ---
     
      CNotify(void);
      ~CNotify(void);

      void Function() { Print("CNotify"); }
      
// ---    
};

How can I properly call the CAdmin version of Function() instead of the CNotify, given that I create only one object of type CNotify?

 
Very bad idea. Why do you want to do that ?
 
Alain Verleyen:
Very bad idea. Why do you want to do that ?
Not a specific reason, I know it'd be as simple as just changing the CAdmin version name, or just do the approach I posted in case I use the function within CNotify and not during run time. However, while coding some stuff I bumped accross the "What if..." thought, and mainly I want to understand if it's possible or not, and if it is, what is the way to do it.
 

Agreed, bad idea. You should never call them.

“When should virtual functions be public, protected, or private?” The short answer is: Rarely if ever, sometimes, and by default, respectively.
          Virtuality - Guru of the Week 9 September 2001.

 
William Roeder:

Agreed, bad idea. You should never call them.

“When should virtual functions be public, protected, or private?” The short answer is: Rarely if ever, sometimes, and by default, respectively.
          Virtuality - Guru of the Week 9 September 2001.

So in short, if I were to use a virtual function, I should only do so within a child class, so I can just use the syntax CBaseClass::Function, but never use it in the run time?
 
Fernando Jose Velasco Borea:

Hey y'all

I'm facing a rather frustrating challenge. Consider the following:

  • CAdmin is a parent class.
  • CNotify inherets CAdmin, therefore, is a child from CAdmin.
  • Both classes have a function called Function(void).
  • CAdmin declaration of Function(void) is virtual.
  • CNotify declaration of Function(void) is not virtual.

Given the following code:

And the following declaration of both classes:

How can I properly call the CAdmin version of Function() instead of the CNotify, given that I create only one object of type CNotify?

You are going to get into a complete mess if you do that sort of thing... BUT, the reason your code was not working is because you had not provided function bodies for the constructor/deconstructor

class CAdmin
{
   public:
                    
      CAdmin(void)   {return;};
      ~CAdmin(void)  {return;};
      virtual void   Function() { Print("CAdmin"); }
};

class CNotify : public CAdmin
{
   public:
      CNotify(void)      {return;};
      ~CNotify(void)    {return;};
      void Function()   { Print("CNotify"); }
};

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{ 
   CNotify notify;
   notify.Function();
   notify.CAdmin::Function();
}
 
Paul Anscombe:

You are going to get into a complete mess if you do that sort of thing... BUT, the reason your code was not working is because you had not provided function bodies for the constructor/deconstructor

Thanks for the info! 

I agree with the fact that most likely by using that it will result into a mess as far as how it's processed. Still, I'd like to know if it is possible, since when I try to compile the suggested code, I still get this error:

'Function' - access to non-static member or function    Class inheritance test.mq4      36      16
 
Fernando Jose Velasco Borea:

Thanks for the info! 

I agree with the fact that most likely by using that it will result into a mess as far as how it's processed. Still, I'd like to know if it is possible, since when I try to compile the suggested code, I still get this error:

the code I posted works fine and does what you asked.

just copy this exactly it is a script.. 

output 

if you still get an error post back your exact code with the error detail

//+------------------------------------------------------------------+
//|                                                      Scratch.mq5 |                 
//+------------------------------------------------------------------+
#property strict

class CAdmin
{
   public:        
      CAdmin(void)   {return;}
      ~CAdmin(void)  {return;}
      virtual void   Function() { Print("CAdmin"); }
};

class CNotify : public CAdmin
{
   public:
      CNotify(void)     {return;}
      ~CNotify(void)    {return;}
      void Function()   { Print("CNotify"); }
};
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{ 
   CNotify notify;
   notify.Function();
   notify.CAdmin::Function();
}
 
Paul Anscombe:

the code I posted works fine and does what you asked.

just copy this exactly it is a script.. 

output 

if you still get an error post back your exact code with the error detail

Somehow I can't get it to compile. Hope this screenshot helps:


 
Fernando Jose Velasco Borea:

Somehow I can't get it to compile. Hope this screenshot helps:


//+------------------------------------------------------------------+
//|                                                     CallBase.mq4 |
//|                             Copyright 2020, Rosh Jardine Capital |
//|                                      https://www.roshjardine.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Rosh Jardine Capital"
#property link      "https://www.roshjardine.com"
#property version   "1.00"
#property strict

class CAdmin
{
   public:        
      CAdmin(void);
      ~CAdmin(void);
      virtual void   Function() { Print("CAdmin"); }
};
CAdmin::CAdmin(void)
{
}
CAdmin::~CAdmin(void)
{
}
class CNotify : public CAdmin
{
   public:
    CAdmin  m_parent;
            CNotify(void);
            ~CNotify(void);
            void Function()   { Print("CNotify"); }
};

CNotify::CNotify(void)
{
}
CNotify::~CNotify(void)
{
}
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{ 
   CNotify notify;
   notify.Function();
   notify.m_parent.Function();
}
you should declare a class member as parent object in public scope..but you've been told..it's bad idea.
 
Fernando Jose Velasco Borea:

Somehow I can't get it to compile. Hope this screenshot helps:


You never said you were on MT4....

I get the same error on MT4 clearly the OOP implementation is different in MQL4

time to let a bad idea go :) 

Reason: