calling a base class virtual function

 

Hi

I'm trying to call a base class's virtual function inside of a derived class with the same function name. Consider the following:

class Parent{
   public:
   virtual void Test(){
      Print("Parent");
   }
};

class Child : public Parent{
   public:
   void Test(){
      Parent* p = (Parent*)GetPointer(this);
      p.Test();
      Print("Child");
   }
};

int OnInit()
{
   Child* c = new Child();
   c.Test();
   return(0);

}

When i do this the program crashes due to stack overflow, because it calls the Test from the child, not from the parent. In C++ you could do something like:

static_cast<Parent>(c).func();

or

c.Parent::Test();

I'm trying to do this because I have a parent function that does some initializations, and I want to do them in the child in a function besides the other initializations with the same name so I can maintain naming conventions.

 

Ok, tried the following that worked. It would be nice to include this in the help :)

class Child : public Parent{
   public:
   void Test(){
      Parent::Test();
      Print("Child");
   }
};
 
GoD2.0:

Ok, tried the following that worked. It would be nice to include this in the help :)

class Child : public Parent{
   public:
   void Test(){
      Parent::Test();
      Print("Child");
   }
};

 

https://www.mql5.com/en/docs/basis/operations/other - Scope resolution operation - example

::Print("parent GetLastError",CCheckContext::GetLastError());
Documentation on MQL5: Language Basics / Operations and Expressions / Other Operations
  • www.mql5.com
Language Basics / Operations and Expressions / Other Operations - Documentation on MQL5
Reason: