Bug (MQL5 compiler): Subclass inheriting from a parrent with the same parent classname in a parent namespace does not compile

 

I have 2 classes with the same name but in different namespaces. When one of those classes (in the sub-namespace) inherits from the other (in the parent namespace), I get compiler errors.


namespace MainSpace
{

   class CMyClass
   {
      public:
         CMyClass()     { }
   };
   
   namespace SubSpace
   {

      class CMyClass : public MainSpace::CMyClass
      {
         public:
            CMyClass(const long id);
      };


      CMyClass::CMyClass(const long id)
      {
      }
   }

}

This code should compile fine. It will, however, give me 9 errors. The first error is "'id' - unexpected token" (all other errors are related to that same constructor)


When removing the subclass' parent like:

...

   namespace SubSpace
   {

      class CMyClass     // : public MainSpace::CMyClass
      {
         public:
            CMyClass(const long id);
      };


      CMyClass::CMyClass(const long id)
      {
      }
   }

...

Then it will compile fine.


==========================================================================================================

I can workaround by defining a different classname for the second class, but that defeats the purpose of inheritance within subnamespaces.

As such a construct is one of the strong properties of namespaces and OOP, I'd love to see this fixed.


The use case why this is such a strong construct:

namespace MainSpace {

   class CMyClass
   {
      public:
         CMyClass()     { }
   };

   namespace SubSpace {

      class CMyOtherClass : public CMyClass
      {
      };

   }     // namespace Subspace
}  // namespace MainSpace

CMyOtherClass will, by default inherit from MainSpace::CMyClass. But when I decide that CMyClass needs to be subclassed, I can simply create a SubSpace::CMyClass (that inherits from MainSpace::CMyClass) without changing any other code. CMyOtherClass will then automatically inherit from Subspace::CMyClass.

As I have a SubSpace in our current project with many small classes (about 40 - 50) that now inherit from MainSpace::CMyClass, I'll have the tedious task of editing all of them.

 
namespace MainSpace
{

   class CMyClass
   {
      public:
         CMyClass()     { }
   };
   
   namespace SubSpace
   {

      class CMyClass : public MainSpace::CMyClass
      {
         public:
            SubSpace::CMyClass(const long id);
      };


      CMyClass::CMyClass(const long id)
      {
      }
   }

}
 
Alain Verleyen #:

Hi Alain, thanks for your response.

I know I can use this workaround (as I mentioned in my original post). But this is just what I wanted to avoid as I now need to include the namespace in about 40 source code files. Hence my request for a (future) improvement.

 
Onno Rijkers #:

Hi Alain, thanks for your response.

I know I can use this workaround (as I mentioned in my original post). But this is just what I wanted to avoid as I now need to include the namespace in about 40 source code files. Hence my request for a (future) improvement.

Probably you want a template class (and need no namespaces):

template<typename T>
class MyClass: public T
{
   ...
};

Then instantiate with MyClass<Main> or MyClass<Sub> (where Sub is derived from Main), as appropriate.