Questions from Beginners MQL5 MT5 MetaTrader 5 - page 192

 

I've started to learn OOP and can't get over the following obstacle. The following script is an example:

CSum result;
void OnStart()
  {
//---
  }
//+----------------------------------------+
class CSum
  {
public:
   int               Calculate(int A,int B);
  };
//---
int CSum::Calculate(int A,int B)
  {
   return(A+B);
  }

Without the "CSum result;" line, the compiler won't generate an error. But it causes an error:

Please tell me what's wrong. I seem to have declared the class object correctly.

 
paladin800:

I've started to learn OOP and can't get over the following obstacle. The following script is an example:

Without the "CSum result;" line, the compiler won't generate an error. But it causes an error:

Tell me what's wrong. I seem to have declared the class object correctly.

A variable of the CSum (result) type is declared before the CSum description is done, which means that the compiler does not know this type yet. Insert CSum at the very beginning of the file.
 
Lone_Irbis:
Also, how much can/should the global variable system be exploited? Is it possible to overload something in this way, or is there a limit? For example, let's say two or more hundreds of variables (of which about half turn into input and back, depending on what piece of code requires testing) and about a dozen and a half small arrays at global level - is it a lot or a little? ^^' And what if there's two or three times more of them as you fine-tune the system? And if we don't have to get so carried away, is there any simpler way to handle data exchange between a dozen different subsystems, many of which require each other's results?
No, there isn't. Global variables are used for other purposes. Use classes to describe subsystems. And you'd better avoid using arrays and global variables altogether.
 
C-4:
A variable of type CSum (result) - is declared before the CSum description is made, which means that the compiler does not know this type yet. Insert CSum at the very beginning of the file.
Oops, it worked. I put the class at the end of the code by analogy with placement of functions. I didn't expect that for a class such an ordering would make a difference.
 
paladin800:
Oops, it worked. I put the class at the end of the code, similar to the placement of functions. I didn't expect that such an ordering would make a difference for the class.
Yes, unfortunately the order of precedence does matter. The hardest case is when two classes use each other simultaneously. Whatever class we introduce first, the second class will be unknown to the compiler and generate an error. In this case you cannot do without class declaration. In your case, it is better to separate CSum into a separate file, for example Sum.mqh and include it using the #include "Sum.mqh" dictation.
 
C-4:
No, you shouldn't. Global variables are used for other purposes. Use classes to describe subsystems. And it's better not to use arrays and global variables at all.
Of course, I understand that it's a good idea to use classes, but I'm still kind of lazy, considering that it's more familiar without them, and they seem to work in any case. But I'm just curious, what's their advantage? Provided that one knows for sure that the code is written by one author exclusively for himself and will never be useful outside a particular program? It has always seemed that classes make sense only if you are writing for someone/something/selling, while for yourself as a hobby it won't make much difference. Apart from aesthetics and general "so-so", is there any practical sense to get involved in all these classes-structures? Speed? Anything else?
 
Lone_Irbis:
Of course I understand that it would be nice to deal with classes, but still I'm too lazy, considering that it is more familiar without them, and they seem to work anyway. But I'm just curious, what's their advantage? Provided that one knows for sure that the code is written by one author exclusively for himself and will never be useful outside a particular program? It has always seemed that classes make sense only if you are writing for someone/something/selling, while for yourself as a hobby it won't make much difference. Apart from aesthetics and general "so-so", is there any practical sense to get involved in all these classes-structures? Speed? Something else?

On the contrary. When you write a custom project, the customer often demands the source code. And then you have to pull functions from classes and insert them into the source code. It is better to give the customer one file instead of dragging a mountain of your libraries, which contain a huge number of functions, which are not used in the work passed to the customer. I.e., it is better to use structured programming on demand.

For your own needs, it's better to use OOP - everything is self-sufficient there and you don't have to bother passing the source code.

 
artmedia70:

On the contrary. When you write a custom project, the customer often requires the source code. And then you have to pull functions from classes and insert them into the source code. It is better to give the customer one file instead of dragging a mountain of your libraries, which contain a huge number of functions, which are not used in the work passed to the customer. I.e., it is better to use structured programming for a customer.

It's better to use OOP for your own needs - all your stuff is there, and you don't have to bother with transferring the source code.

Hmmm... Well, perhaps so :) That is, of course, the principle looks tempting... in theory. Especially taking into account that I can't make a single file without any structures or classes. The thing is, I write mostly out of interest, testing my own random theories and inventing endless bicycles. In parallel, I only study what is beginning to be required to implement the idea. All this is happening within the framework of a single learning and experimental Expert Advisor - initially a simple martin, but now it's more of a multifunctional scalper in the bud (and already theoretically profitable). So, at some point the robot became trivially too big. >.> When most of the time was spent just scrolling the mouse wheel in search of the right piece of code, I got the "genius" idea to make it into separate files (13 parts connectable at the moment), just grouping functions by their general concept. Like news parser here, level processing there, another one with moose controllers, statistics separately, etc. But at that time my enthusiasm to deal with OOP was not enough...

So my problem, apparently, is that I just grab every idea I come up with and modify it on top of an existing robot in a rather... chaotic sequence. The result is rather bizarre, with all sorts of switches and combinations of modes, many of which are left undone. The whole picture is complemented by a hundred and fifty global variables, which have to be removed from inputs, just to not take so much time, being imprinted in the tester visualizer at the beginning. Plus, of course, mountains of rubbish and rudiments of abandoned or redesigned ideas.

And it seems to be a good time to sort through all this heap of rubbish and put everything into classes (and firstly read through at least one article on OOP without falling asleep in the process)... But I'm confused by the amount of work to be done and, ahem, its relation to potential sense of the whole thing. That is to conclude all in classes - it seems not so volumetric task... But would it make sense, if, for example, to dump everything in a row into public, ignoring all these private/protected? How is that better than having a folder with a bunch of .mph's and a dozen of common functions each, if they all fit in one robot anyway?

 
Lone_Irbis:

I would advise you to make a single template, which already has all the necessary actions for initialisation, connection, collection of always needed data, etc...

An unexpected idea came to mind - load a template, rename it and write in it only what is relevant to that particular idea. And those functions that you always use, in any code, returning the same data in any situation - put them in classes. And everything will fall into place at once. You can also structure directories. In \experts\ create (I have it that way) the folder Orders, where I also put all the files belonging to different customers in separate folders, there is a folder Ideas, Tests, etc.

In this way, you can keep things in order.

 
Unfortunately, even by formally learning OOP, you will not be able to build an OOP program. Rather, you need to get into the philosophy of this approach, and this is the next level after getting formal knowledge. So it turns out, do you really need it? But if you ask questions how to do it better, it means that you feel that the way you've chosen is not optimal. In any case, the choice is yours.
Reason: