Questions on OOP in MQL5 - page 44

 
Igor Makanu:

I disagree:

Please update your knowledge of what "assignment operator" and "copy constructor" are, how they differ, when and how they are called (explicitly and implicitly).
In the code you have highlighted in yellow the return of a local variable from a function, which results in calling the default copy constructor (not to be confused with the assignment operator).

What a "full data type" is, unfortunately, is not entirely clear, nor is what you were trying to prove with your code...
I'll repeat:"thedefault MQL assignment operator returns void;"

struct A{
   int x,y;
};

void OnStart(){
   A a, b;
   Print(typename(a = b));   // void
}
 
Koldun Zloy:

Wrong.

Just define your operator=, which returns this, and see the difference.

You can see the difference when the result of the assignment is assigned again.

If I define operator=, I miss my initial goal of calling the default ::=

It seems to be an unsolvable question.


Sergey Dzyublik:

What "full data type" is, unfortunately, is not quite clear, neither is what you were trying to prove with your code...

full is full - the data structure will be preserved and there's no need to control copying of structures if I add new fields

 
Igor Makanu:

If I define operator =, I get away from my initial goal of calling the default ::=

Do you really need this kind of assignment?

b = c = a;
 
Igor Makanu:

complete is complete - the data structure will be preserved and there is no need to control copying of structures if new fields are added

Then, according to your terminology, calling the default assignment operator could give an "incomplete data type".
The bug from 2019.05.03 was never fixed:https://www.mql5.com/ru/forum/1111/page2451#comment_11556395

 
Koldun Zloy:

Do you really need such an assignment?

purely in theory, I want to know if it's a "new entity" rather than an operator =

I don't need it for practical use at all, I have no problem putting it into 2 operators


Sergey Dzyublik:

Then, according to your terminology, calling the default assignment operator can give an "incomplete data type".
The bug from 2019.05.03 has never been fixed:https://www.mql5.com/ru/forum/1111/page2451#comment_11556395

"my terminology" is to ask, no complaints,

but the point of using the default operator = is that it is convenient to describe only the fields in the structure, and everything is copied, including even the arrays dimensions (although only with increasing dimensions - the principle of work is the same as in ArrayCopy() )


Well, if it's a bug, it must be so far.

 

the question is purely theoretical:

have seen, possibly in SB, a constructor call like this:

class A{
public:   
   A(){Print(__FUNCTION__);}
};

class B{
public:   
   A a1,a2;
   B():a1(),a2() { Print(__FUNCTION__); }   
};

how would this code be different:

class A{
public:   
   A(){Print(__FUNCTION__);}
};

class B{
public:   
   A a1,a2;
   B() { Print(__FUNCTION__); }   
};

I have unzipped it, I don't see any difference, then be a bit more specific - what or why can we use a forced constructor call for a1 and a2 objects?

what is the "convenience" of the first option?

 
Igor Makanu:

the question is purely theoretical:

have seen, possibly in SB, a constructor call like this:

how would this code be different:

I have unzipped it, I don't see any difference, then be a bit more specific - what is the benefit or why can we use a forced constructor call for objects a1 and a2?

what is the "convenience" of the first option?

Constructors can have parameters and they have to be passed somehow.

That is, until there are no parameters, there is no difference, but if you use constructor A(int arg), it's a different story

 
Maxim Kuznetsov:

constructors have parameters, and they must be passed somehow

That is, until there are no parameters, the difference is insignificant. But if you have constructor A(int arg), it is a different story

You're probably right - it depends on the purpose

I just had difficulties with the first variant, when I wanted to use 2 constructors of class B and received duplicated code in both constructors - I removed them, and all I got was a part of code that was in the first variant, and the question arose where I saw it and why I wrote it that way )))

 
Igor Makanu:

what gives or why can the constructors of objects a1 and a2 be forced to use?

Correct process name is not "forced constructor call a1 and a2", but initialization of class (non-static) fields.
Obligatory in cases of:

- Using constant class fields;
- the absence of a default constructor for the class fields;
- absence of any other way of initialization of objects other than through constructor call (for example, assignment operator is removed);
-...

 

Can someone explain how this initialisation of the fields is better than this:

CArray::CArray(void) : m_step_resize(16),
                       m_data_total(0),
                       m_data_max(0),
                       m_sort_mode(-1)
  {
  }

is better than this:

CArray::CArray(void)
  {
        m_step_resize=16;
        m_data_total=0;
        m_data_max=0;
        m_sort_mode=-1;
  }

And what is the point anyway?