Deprecated behavior - why? - page 2

 
Alain Verleyen:
That's the fast fix, I am suggesting to think better about the design.

Of course, you are right and I agree with you.

 
Thank you guys for enlightening me :)
 
Petr Nosek:

The new behaviour means that the called parent method is hidden by the descendant method. You have to explicitly specify the instance of the called method in order to avoid a warning.

This won´t compile in MT4 so it´s not a good solution, when your code needs to be 100% compatible between both platforms. 

 
Doerk Hilger:

This won´t compile in MT4 so it´s not a good solution, when your code needs to be 100% compatible between both platforms. 

If you need same-named functions in both classes you have to use this design IMO that can be compiled both in MQL5 and MQL4:

class CBase
   {
      public:
      bool Text(string text, int index=0) { return true; }
      string Text(int index=0) { return "CBase"; }
   };
   
class CDev : public CBase
   {
      public:
      string Text(string text1, string text2) { return "CDev"; }
      string Text(int index=0) { return CBase::Text(index); }
   };

void OnStart()
  {
   CDev dev;
   string value=dev.Text();
   Print(value);
  }
 
Petr Nosek:

Of course, you are right and I agree with you.

Actually I was wrong, it depends of the situation. The simple fix using scope operator can be the right solution or not.

Example:

class CBase
  {
   int               rawdata[];
public:
                     CBase(void) { /*...*/ };
                    ~CBase(void) {};

   void              Load(string filename)
     {
      /* save raw data */
     }
  };
class CDerived : public CBase
  {
   string            filename;
public:
                     CDerived(void) { filename="..."; };
                    ~CDerived(void) {};

   void              Load(void)
     {
      CBase::Load(filename);      // deprecated behavior fixed simply with CBase::

      /*
         some stuff
      */
     }
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   CBase base;
   base.Load("base...");      // Ok

   CDerived derived;
   derived.Load("derived..."); // deprecated behavior... Is actually a logical error Load() should be used.
  }

The Base class can be used standalone, so the load method with a filename parameter is fine.

In the derived class, I have a load method which is a kind of wrapper on the CBase Load method. However on a derived object I don't want the CBase Load method to be callable as it would lead to logical error, so I used intentionally the "hidden method".

Inside CDerived the simple fix is what is needed, and outside I get the warning, reminding me it's a logical error the call this version of the Load() method.

On other case, a redesign would be needed, all depends of the semantic, which is not obvious with meaningless example using Text() method.

C++: Name Hiding - Programmer and Software Interview Questions and Answers
C++: Name Hiding - Programmer and Software Interview Questions and Answers
  • About
  • www.programmerinterview.com
Name hiding in C++ is best illustrated by an example – so take a look at this simple code below and then read on for an explanation: Name Hiding in C++ Example It looks like the call to the someFunc function in the main function in the code above should run just fine. But, in reality that method call results in an error which will say...
Reason: