Errors, bugs, questions - page 759

 
WWer:

So it should be so that the role of copy constructor is performed by overloaded assignment operator?

I don't know what's right, but the assignment operator in VS does not perform the role of the copy constructor.
class tt
{
public:
        int f;
        tt(tt& u) {f = 1;}                // Копирующий конструктор, при выполнении его f == 1
        tt(){}
        void operator=(tt &u){f = 100;}   // При выполнении f == 100
};

tt fn()
{
        tt q3;
        return(q3);
}

int _tmain(int argc, _TCHAR* argv[])
{
        tt q2 = fn();
        std::cout<<q2.f;
        char input[54];
        std::cin>>input;
        return 0;
}

The screen displays 1, i.e. the copying constructor is executed.

I think it's this: "2) uncommenting a stop with an overloaded assignment operator" is not supposed to help.

 
220Volt:
I don't know what is correct, but the assignment operator in VS does not perform the role of the copy constructor.

The screen displays 1, i.e. the copy constructor is running.

I think this: "2) uncommenting a stop with an overloaded assignment operator" should not help.

The copy constructor is executed here

tt q2 = fn();
not inside the fn() function.
 
mql5:

The copy constructor is performed here

and not inside the fn() function.

In this variant:

class tt
{
public:
        int f;
        tt(tt& u) {f = 1;}
        tt(){f = 198;}
        void operator=(tt &u){f = 100; std::cout<<"Выполнился присваивающий конструктор";}
};

tt fn()
{
        tt q3;
        return(q3);
}

int _tmain(int argc, _TCHAR* argv[])
{
        tt q2 = fn();
        std::cout<<q2.f;
        char input[54];
        std::cin>>input;
        return 0;
}
The line "Assignment constructor is executed" is not output.
 

And that's how it's deduced:

tt q2;
q2 = fn();
WWer, if by analogy with VS, you need to look at the specific situation, give a specific example of copying/assignment. What will be executed is decided on the spot.
 
WWer:

Why should it be initialised? It's an instance of a class that has a constructor for just that purpose.

Yeah, really, sorry.


And how to write a correct copying constructor for this purpose?

Your function getCopy() that is trying to return the object is quite correctly written. After overriding the assignment it already returns a pointer, the error evaporates.

--

The confusion here may be caused by the fact that the language has some "syntax backlash", associated with the same syntax representation of accessing the fields of a static object and a dynamic one (with a point instead of an arrow, which, by the way, is really convenient). This is very commonplace, and can somewhat blunt the distinction between objects and object pointers, which in turn can lead to some illusions, such as that something can be assigned to a static (or automatic) object variable. Far from it, an object variable to the left of an assignment is always a pointer, if the copy constructor is not defined.

 
Unfortunately, this is where MQL5 differs from C++.

C++ constructs q2 object inside fn() function in return, that's why copy constructor works.

In MQL5, the object is constructed from the outside, so there are differences. Let's eliminate them.


For those who are interested: it's called Return value optimization

 

Colleagues, good day!

Can you tell me how I can get price values for different financial instrumentsin sync? I mean, for example, closing prices of bars at a certain point in time in the past on different instruments. The use of CopyClose function and getting the values by index of bars is not absolutely correct, because there can be some missed bars for different instruments. As far as I remember, mql4 has the BarShift function by time, is there an analogue on mql5?

Документация по MQL5: Доступ к таймсериям и индикаторам / CopyClose
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyClose
  • www.mql5.com
Доступ к таймсериям и индикаторам / CopyClose - Документация по MQL5
 

If you know the time, you can use this function.

Addressing by start date and number of items required

intCopyClose(
stringsymbol_name,// symbol name
ENUM_TIMEFRAMEStimeframe,// period
datetimestart_time,// from whichdate
intcount,// how many we copy
doubleclose_array[]// array for copying closing prices
);

 
Karlson:

If you know the time, you can use this function.

Refer to the start date and number of items required

intCopyClose(
stringsymbol_name,// symbol name
ENUM_TIMEFRAMEStimeframe,// period
datetimestart_time,//from what date
intcount,// how many we copy
doubleclose_array[]// array for copying of closing prices
);

Suppose, I want to find ratio of close[i] EUR/USD and close[i] GBP/USD, but if I use index i, the bars may be different in time anyway, because there are some missed bars.

In other words, I have to use CopyClose(Symbol, 0, date_of_current_bar, 1, array) in cycles for each bar ?

Документация по MQL5: Доступ к таймсериям и индикаторам / CopyClose
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyClose
  • www.mql5.com
Доступ к таймсериям и индикаторам / CopyClose - Документация по MQL5
 

It seems to me, that bars of older TF rarely "disappear".

Another variant is to obtain the time of this bar simultaneously through CopyTime().

And third option (probably easiest) use CopyRates in structure at once.

Документация по MQL5: Доступ к таймсериям и индикаторам / CopyRates
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyRates
  • www.mql5.com
Доступ к таймсериям и индикаторам / CopyRates - Документация по MQL5