Features of the mql5 language, subtleties and tricks - page 283

 
Vladislav Boyko #:

I'd probably prefer to wrap it in a method.

I added a1/a2 on purpose to show that wrapping in a method is sometimes almost impossible - too artificial with complex branching inside for from different local variables.

 

Vladislav Boyko #:

reference (the pointer is converted by the compiler into a reference to be passed into operator=(const A&))

This is how you can see its presence/signature.

class A {};

A a = NULL;
'NULL' - parameter conversion not allowed
   void A::operator=(const A&)
'operator=' - object required
2 errors, 0 warnings
 
fxsaber #:

That way you can see its presence/signature.

This is all good, but up to the first constant field. It is probably better to add an explicit copy constructor and use it. But in MQL there are 2 disadvantages concerning copy constructors:

  1. the compiler does not generate an implicit copy constructor

https://www.mql5.com/en/docs/basis/types/classes#class

If your constructor is explicitly defined, the initialization of a structure or class variable using the initializing sequence is impossible.

 
Vladislav Boyko #:

That's all well and good, but up to the first constant field. It is probably better to add an explicit copy constructor and use it. But MQL has 2 disadvantages concerning copy constructors:

  1. the compiler does not generate an implicit copy constructor

I don't get it.

 
fxsaber #:

I don't get it.

https://www.mql5.com/ru/forum/1111/page3600#comment_55388475

Implicit is the one you didn't declare, but the compiler generated it for you. MQL compiler doesn't know how to do that.

https://en.wikipedia.org/wiki/Copy_constructor_(C%2B%2B)

Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one.

The following examples illustrate how copy constructors work and their necessity.

#include <iostream>

class Person
{
    public:
        int age;
        Person(int age) : age(age) {}
};

int main()
{
    Person timmy(10);
    Person sally(15);

    Person timmy_clone = timmy;
 
    std::cout << timmy.age << " " << sally.age << " " << timmy_clone.age << std::endl;
 
    timmy.age = 23;
 
    std::cout << timmy.age << " " << sally.age << " " << timmy_clone.age << std::endl;
}

As expected,  timmy  has been copied to the new object,  timmy_clone . While  timmy 's age was changed,  timmy_clone 's age remained the same. This is because they are totally different objects.

The compiler has generated a copy constructor for us, and it could be written like this:

Person(Person const& copy)
  : age(copy.age) {}

 
Vladislav Boyko #:

https://www.mql5.com/ru/forum/1111/page3600#comment_55388475

Implicit - one that you didn't declare, but the compiler generated it for you. MQL compiler does not know how to do this.

Difficulties only with const fields?

 
fxsaber #:

Difficulties only at const fields?

Yes, the disadvantage of operator= is that you can't use it for const fields (personally I have more than half of my objects contain const fields). And you have to declare the copy constructor yourself(unlike operator=).

 
How do I know that a chart window is in focus and not another window with another chart open?
 
Andrei Iakovlev #:
How do I know that the chart window is in focus and not another window with another chart open?

Answer:

bool IsChartFocused()
  {
        return(bool(ChartGetInteger(0,CHART_BRING_TO_TOP)));
  }
 

In the next versions of MQL5 I would like to have the ability to launch services.

I need a function that works from Expert Advisor scripts, which will launch the selected service with parameter passing.