PLO. Application issues - page 12

 
GreyCardinal:

You want (as I think is obvious) - to get overridden names in filename variables...

For Init() to work properly, it must be called after constructing the object.

Or not?

I think the VMT is only initialised in the terminating code of the constructor, so when you call a method in the constructor, it will call a method of type variable, not of type constructible object.

That's something like this.

At least this one works:

void OnStart()
  {
   CO2=new CCO2;
   CO2.Init();
   Print(CO2.Name()," filename=",CO2.filename);
   delete CO2;
   
   H2O=new CH2O;
   H2O.Init();
   Print(H2O.Name()," filename=",H2O.filename);
   delete H2O;
  }

To MetaQuotes: If I got it right, it's better to prohibit calling virtual functions in constructors altogether. To.

Or make them work correctly. Which I doubt.

 

Just getting to know parametric constructors. But it seems that in the example

//+------------------------------------------------------------------+
//| класс для хранения фамилии и имени персонажа                     |
//+------------------------------------------------------------------+
class CPerson
  {
   string            m_first_name;     // имя 
   string            m_second_name;    // фамилия
public:
   //--- пустой конструктор по умолчанию
                     CPerson() {Print(__FUNCTION__);};
   //--- параметрический конструктор
                     CPerson(string full_name);
   //--- конструктор со списком инициализации
                     CPerson(string surname,string name): m_second_name(surname, m_first_name(name)) {};
   void PrintName(){PrintFormat("Name=%s Surname=%s",m_first_name,m_second_name);};
  };
The parentheses in the highlighted line are incorrectly placed.
 
Yedelkin:

Just getting to know parametric constructors. But it seems that in the example

in the highlighted line has the wrong parentheses.
Thank you, we will correct it.
 

Technical post (start of a small discussion on initialisation, also in constructors). Moved, so it doesn't get lost under the heavy footsteps of the newbies: https://www.mql5.com/ru/forum/58/page31#comment_66890

Изучаем и пишем вместе на MQL5
Изучаем и пишем вместе на MQL5
  • www.mql5.com
2) вывод всей возможной информации по инструментам, ордерам и др.
 

A question about initialisation. As a general rule, initialization of string variables and complex objects is done automatically, "by default". This wastes a certain amount of computing power of computer (I call it "computer clock", sorry for amateurish terms).

Now there are initialisation lists that allow you to initialise variables with custom values. Is there any difference in initialisation speed between automatic initialisation and initialisation with a custom value? In other words, does the number of "computer strokes" when using initialisation lists (and custom values) increase compared to automatic initialisation?

 
Yedelkin: In other words, does using initialisation lists (and custom values) increase the number of "computer cycles" compared to automatic initialisation?
And if a few clock cycles are saved, does this give a significant gain ? Especially since initialisation is done only once, and the values will still have to be assigned later.
 
Valmars:
And if we save a few clock cycles, will it give a significant gain? Especially since the initialization is done once, and the values will still have to be assigned later.
The answer is not on the merits. Personally, the question is important to me, because I want to understand the technique. Every single clock cycle is important. Otherwise there would be no question. We know how to rivet teapots without regard to the rules.
 
Yedelkin:
This is an important question for me personally, because I want to understand the technique.

The right approach, everything would be like that. Initialization lists are a consequence of the introduction of parametric constructors.

Let's examine two examples:

class A
{
private:
   // конструктор по умолчанию недоступен
   A(){}
public:
   A(const double& value)
      : m_Value(value)
   {}
private:
   double m_Value;
}

class B
{
public:
   B()
   // т.к. коструктор А не имеет конструктора по умолчанию, единственный способ
   // использования класса -- инициализация в списке инициализации.
   // по-другому просто не должно скомпилиться
      : A(0)
   {
   }

private:
   A m_Value;
}

It is all written in the comments.

Example 2:

class A
{
public:
   A()
   {
      Init(0);
   }
   
   A(const double& value)
   {
      Init(value);
   }
   
   void Init(double value)
   {
      // очень тяжелая функция
   }
};

class B
{
public:
   // вариант1, правильный
   B(const double& value)
      : m_Value(value)
   {
   }

/*
   // вариант2, неправильный
   B(const double& value)
   {
      m_Value.Init(value);
   }
*/

private:
   A m_Value;
};
So, in variant 1 we have 1 call of Init and in variant 2 we have 2 calls. And since the "like" function is very heavy, there's an upside.
 

TheXpert, thank you very much! I'll look into it.

I understood indirectly from your words, that there is no difference in speed between auto-initialization of variable of " string, dynamic array and complex object" type and user initialization of the same variable.

 
Yedelkin:

I've implicitly understood from your words that there is no difference in speed between auto-initialisation of a variable of " string, dynamic array and complex object" type and user initialisation of the same variable.

Show me an example, so that there is no confusion, then I will answer.
Reason: