Questions on OOP in MQL5 - page 32

 
Vladimir Tkach:
Lately I've started to see some kind of "rubbish collection" in the functions of various compilers. What is it?
If you mean garbage collection, it is the thing that does delete instead of you, but not when the variable leaves the scope, but when it wants to. By the way, not the fact that you're allowed to call the destructor yourself, it will depend on the language, in Sharp, as I understand it's impossible.
 
Vladimir Simakov:
Here it will depend on the language, in Sharp, as I understand it is impossible.

It is, all that can be done is to assign an unused object to NULL, there are manuals on Microsoft site how to force to call the rubbish collector, but it seems that I read on hubra that not the fact that the collector will be launched in this call as expected

that is, in Sharp they have taken most of the memory management away from the programmer - everything is at the mercy of the OS and the compiler

 
Igor Makanu:

It is, all you can do is assign an unused object to NULL, there are manuals on Microsoft site to force the rubbish collector, but it seems that I read on hubra that not the fact that the collector will be launched in this call as expected

that is, in Sharp they have taken most of the memory management away from the programmer - everything is at the mercy of the OS and the compiler

There you go. I'm writing my first Sharp task right now and after new in method I feel dreadful because of absence of delete in code))).
 
Vladimir Simakov:
There you go. I'm currently writing my first Sharp textbook and I'm creeped out by absence of delete in code after new in method))).

instead of delete MyObj; write MyObj = null; - this will work

and then use ?? - it was a bit awkward at first, then it works automatically

but it's not the biggest difference from C++, in C# all objects are references and if you set MyObj_1=MyObj_2; you don't get a copy, you get a pointer... I'm about to confuse you, here's a quick tutorial, I'm just looking over it in case I've forgotten anything

https://metanit.com/sharp/tutorial/2.16.php


Типы значений и ссылочные типы | C#
  • metanit.com
Ранее мы рассматривали следующие элементарные типы данных: int, byte, double, string, object и др. Также есть сложные типы: структуры, перечисления, классы. Все эти типы данных можно разделить на типы значений, еще называемые значимыми типами, (value types) и ссылочные типы (reference types). Важно понимать между ними различия. Ссылочные типы...
 

I think I'm beginning to understand the necessity and usefulness of OOP, but I'm having trouble implementing it.

I have a class that is to be used with three different sets of variables. But it performs one task. In simple terms, we can insert 3 constructors, declare 3 variables and refer to them. But as I see it, it is not quite correct. On top of that, there is one variable of the string type in two variants but it is different in name and is used in different parts of the code. Of course, you may change the sequence of variables but I think this is not quite correct either.

I read the documentation about the new operator but do not understand how it may help. I do not see the difference between three different object variables and three pointers to the same objects. Perhaps it's profitable when you create a pointer once, use this object and delete it when it is not needed. But if you need the object regularly, it's absolutely silly to create a pointer to it each time and delete it.

So, please give me a hand. I've read some explanations of OOP in C++, but they are described there in a worse way than in the documentation on mql5. Please do not ask the impatient ones to join me, I will ask too many questions. I'm not interested in ready-made code without explanations either, I want to understand it, and not to stupidly repeat "Do with us, do as we do...".

 
Alexey Viktorov:

I think I'm beginning to understand the necessity and usefulness of OOP, but I'm having trouble implementing it.

I have a class that is to be used with three different sets of variables. But it performs one task. In simple terms, we can insert 3 constructors, declare 3 variables and refer to them. But as I see it, it is not quite correct. On top of that, there is one variable of the string type in two variants but it is different in name and is used in different parts of the code. Of course, you may change the sequence of variables but I think this is not quite correct either.

I read the documentation about the new operator but do not understand how it may help. I do not see the difference between three different object variables and three pointers to the same objects. Perhaps it's profitable when you create a pointer once, use this object and delete it when it is not needed. But if you need the object regularly, it's silly to create a pointer to it each time and delete it.

All in all, please help. I've read some explanations of OOP in C++, but they are described there in a way worse than in the documentation on mql5. Please do not impatient people, I will ask too many questions. I'm not interested in ready-made code without explanations either, I want to understand it, and not to stupidly repeat "Do with us, do as we do...".

We need more details. What task does the class perform?

 
Koldun Zloy:

Details are needed. What task does the class perform?

It's no secret, a sample of economic calendar events CalendarValueHistory(). In one case, all the news in the time range. In another by country and another time range. In the third one it's by a symbol. But is there a fundamental difference? Do programming lectures divide solutions by the type of tasks they perform?

I recently met someone who failed a driving test and was indignant, saying that they had not driven at that intersection in their driving lessons and simply did not know how to drive at that intersection.

 
Although now I'm thinking that maybe it's really better to create a pointer, use the object and delete it when it's not needed? After all, the time range will be different every time. Or is it easier to use setters to insert time into an existing object?
 
Alexey Viktorov:

In another by country and a different time range.

countries in enum

In the constructor, even through the usual switch - case, do the initialization as you need, I showed above a template with interfaces, I also initialize one of the strategies in the constructor, depending on the parameters, and the rest of the work is reduced to writing a specific strategy

https://www.mql5.com/ru/forum/85652/page24#comment_13054686

 
Igor Makanu:

countries in enum

In the constructor, even through the usual switch - case, do the initialization as you need, I showed above a template with interfaces, I also initialize one of the strategies in the constructor depending on the parameters, and the rest of the work comes down to writing a specific strategy

https://www.mql5.com/ru/forum/85652/page24#comment_13054686

Boring... There are templates and interfaces to be mastered as well???