Inaccurate documentation on constructors

 

Dear friends, 

Just to let you know (and perhaps MetaQuotes people could correct errors). I refer to https://www.mql5.com/en/docs/basis/types/classes#class, the article of the documentation that refers to structs, classes and their constructors (among others).

There is a sentence that says: 

"The constructor, all parameters of which have default values, is not a default constructor."


And there is an example there, of which I report just a snippet of code:

//+------------------------------------------------------------------+
//| A class with a default constructor                               |
//+------------------------------------------------------------------+
class CFoo
  {
   datetime          m_call_time;     // Time of the last object call
public:
   //--- Constructor with a parameter that has a default value is not a default constructor
                     CFoo(const datetime t=0){m_call_time=t;};
   //--- Copy constructor
                     CFoo(const CFoo &foo){m_call_time=foo.m_call_time;};
 
   string ToString(){return(TimeToString(m_call_time,TIME_DATE|TIME_SECONDS));};
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
// CFoo foo; // This variant cannot be used - a default constructor is not set
//--- Possible options to create the CFoo object
   CFoo foo1(TimeCurrent());     // An explicit call of a parametric constructor
   CFoo foo2();                  // An explicit call of a parametric constructor with a default parameter
   CFoo foo3=D'2009.09.09';      // An implicit call of a parametric constructor
   CFoo foo40(foo1);             // An explicit call of a copy constructor
   CFoo foo41=foo1;              // An implicit call of a copy constructor
   CFoo foo5;                    // An explicit call of a default constructor (if there is no default constructor,
                                 // then a parametric constructor with a default value is called)
...

And later also the declaration of an array of the class:

// CFoo foo_array[3];         // This option cannot be used - a default constructor is not specified

Well, the author of the documentation goes on:

If you uncomment these strings

  //CFoo foo_array[3];     // This variant cannot be used - a default constructor is not set

or

  //CFoo foo_dyn_array[];  // This variant cannot be used - a default constructor is not set

then the compiler will return an error for them "default constructor is not defined".


Well, jsut try it and you will see: This statement is WRONG and the compiler can produce a very well-funcioning script if you uncomment those lines!


  • The author should explain me, in the "larger" snippet of code, which the difference is supposed to be between

CFoo foo;

and 


CFoo foo5;

some lines below. The comment says: if there is no default constructor, then a parametric constructor with a default value is called. 

And this is what exactly happens both for the instantiation of foo, and for the creation of the foo_array. In fact all the code brokes together only when we change the CFoo constructor by removing the default value from t:

CFoo(const datetime t)

So, I am sorry, and if anybody can provide an alternative explanation I will be happy. But, to me, it looks like that the documentation is wrong, and a user-defined constructor with parameters having a default value behaves exactly as a default constructor (without parameters).


If anybody is around, please correct the documentation...

Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  • www.mql5.com
The structure name can't be used as an identifier (name of a variable or function). It should be noted that in MQL5 structure elements follow one another directly, without alignment. In C++ such an order is made to the compiler using the following instruction: If a structure contains variables of the string type and/or object of a dynamic...
 

It usually means that after some future version upgrade, it can start to behave according to the documentation.
They wrote it like this because they planned constructors to behave as is written. Nobody outside MQ can't know if this will be a new feature or they'll consider it a bug and fix it. 

Use it as is documented or be ready for a possible surprise in the future. 

 
Fab: There is a sentence that says: 

"The constructor, all parameters of which have default values, is not a default constructor."

But, to me, it looks like that the documentation is wrong, and a user-defined constructor with parameters having a default value behaves exactly as a default constructor (without parameters).

If anybody is around, please correct the documentation...

When classes were introduced into MT4 Build 600 (2014.02.03) you couldn't create arrays of them without a default constructor. Now in Build 1280 (2020.07.24) you can.

This is a user's forum not MetaQuotes. No one here can correct the documentation.

 
William Roeder:

When classes were introduced into MT4 Build 600 (2014.02.03) you couldn't create arrays of them without a default constructor. Now in Build 1280 (2020.07.24) you can.

This is a user's forum not MetaQuotes. No one here can correct the documentation.

Thank you so much William. I did not want to "complain", but to let other people like us knowing about this inaccuracy. Can you suggest how would it be possible to contact MetaQuotes for having them taking care of such things and maintain documentation current?

Reason: