Can't inherit a base class with parameterized constructor

 

Hi,

I've been looking for this answer in the forums but most articles and topics are dated since MQL5 accepted only the default class constructor. Now it is possible to have different constructors with different parameters. But, try to inherit a base class whose only constructor has arguments:

class myClass1
{
        private:
        string mystring1;
        public:
         myClass1(string);
};

myClass1::myClass1(string s)
{
        mystring1 = s;
}

class myClass2 : myClass1
{
        private:
        string mystring2;
        public:
        myClass2(string);
};

myClass2::myClass2(string s) // Compiler complains at this line making a wrong reference to myClass1.
{
        mystring2 = s;
}

Paste this code in a module, it won't compile. The compiler's output at the indicated line: 'myClass1' - wrong parameter count.

The logic would be to call the base class constructor from myClass2 constructor but don't know if that's possible.

How do we deal with this kind of ambiguity? Should we just stick to default constructors and use function initializers?

This should be included in the documentation or the compiler should provide a different error message if it detects this situation (and it won't be supported by the MQL spec).

If you got this already solved it please let me know.

Thanks guys!

 

Do you mean this?

class myClass1
  {
private:
   string            mystring1;
public:
                     myClass1(string);
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
myClass1::myClass1(string s)
  {
   mystring1=s;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class myClass2 : myClass1
  {
private:
   string            mystring2;
public:
                     myClass2(string);
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void myClass2::myClass2(string s):myClass1(s) // Compiler complains at this line making a wrong reference to myClass1.
  {
   mystring2=s;
  }

 

 
alexvd:

Do you mean this?

 

Yes sir, this is the answer i was looking for, it works great. BIG THANKS!

It's curious how MQL5 handles this constructor cascade calling. Most languages i've coded with use explicit calls to base class constructors inside the derived class constructor but i really liked this approach much, it is genius.

This should be mentioned in the official documentation as it is part of the MQL5 specification.

 
TripleHeinz:

Yes sir, this is the answer i was looking for, it works great. BIG THANKS!

You also can simplify your code using initialization lists like in this example.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class myClass1
  {
private:
   string            mystring1;
public:
                     myClass1(string s):mystring1(s) { }
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class myClass2 : myClass1
  {
private:
   string            mystring2;
public:
                     myClass2(string s):myClass1(s),mystring2(s) { }
  };
//+------------------------------------------------------------------+

 

This should be mentioned in the official documentation as it is part of the MQL5 specification.

 

It has already written here

 

 
TripleHeinz:
Thanx for the question!
 

VincentX:
Thanx for the question!

Haha, indeed :) I came across exactly the same problem today - and it was a nice surprise to find out that someone has already asked for a solution.

 
Pawel Wojnarowski:

Haha, indeed :) I came across exactly the same problem today - and it was a nice surprise to find out that someone has already asked for a solution.

Totally, this question and answer just saved me six years later from exactly the same problem! THanks guys

 
Thank you, good people! This is helpful!