Questions on OOP in MQL5 - page 43

 
Igor Makanu:

SZS: Your posts are almost always reasonable, but the presentation of the material, well, not exactly with good intentions, of course your business, but imho, you want to help - help, want to be clever, well, it often turns out that way

The pitch makes you think... you wrote that you've begun to think about the code, and before you were coding at the drop of a hat...

 
A100:

The pitch makes you think... you wrote that you started thinking about the code, and before that you were coding in a rush.

hmm... well I've never written a doddle! - that's a fact!

SZZY: I update the topic periodically not because of ignorance or because I can not find the material myself but because there is an agent whose opinion is interesting to me ;)

ZSYS: the result of communication - rather rethought why use modifiers, why we need a get/set - it saves time during the development phase, with the skillful use of the compiler, testing the written code will be immediately successful - here really opened my eyes, who was an asset in the topic, once again, thank you!

 
Koldun Zloy:

Here's an example:

From your code, it is not clear what the interface is for, so I threw it out.

Of course I don't know your task completely, but there's a 99.99% chance it's not needed.

Wicked...

 
Igor Makanu:

Um... I've never written in a blur! - that's a fact!

SZZY: I periodically update this topic not from ignorance, or because I can not find material on my own, but because there is an asset, whose opinion I am interested ;)

SZZY: the result of communication - rather rethought why use modifiers, why we need a get/set - it saves time at development stage, with skillful use of the compiler, testing the written code will be immediately successful - here really opened eyes, who was an asset in the topic, once again, thank you!

It's very good that you have learned something. But you can make much more progress if you read Stroustrup's C++.

But you'd better not read articles and various videos on Habra. Anybody may write there. They will only confuse you.

 

A question has arisen.


There is a panel with buttons, each button will execute some functions. The functions called by pressing one of the buttons, let's call it Exit1, should be placed in a separate class CExit1.

But these functions use some global variables and methods of other objects.

I have not participated in large projects. I would like to know expert opinion, how to do it correctly?

I often program in such a way that if an mq4 file is compiled, all classes and include-files are loaded without errors and the EA works properly. However, if I compile a class separately, I get warnings because some external variables and functions are not visible.

Here is my question. Does a class have to compile itself without errors, I mean, it has to contain all of the copies of EA's global variables, object references, etc. that are used? If so, when would be the best time to do this? If there are a lot of interacting objects, how to arrange them so that there is less confusion? Do you create diagrams of your programs if they proliferate?

Share how you write and implement work with your objects.

 
Vasiliy Pushkaryov:

A question has arisen.


There is a panel with buttons, each button will execute some functions. The functions called by pressing one of the buttons, let's call it Exit1, should be placed in a separate class CExit1.

But these functions use some global variables and methods of other objects.

I have not participated in large projects. I would like to know expert opinion, how to do it correctly?

I often program in such a way that if an mq4 file is compiled, all classes and include-files are loaded without errors and the EA works properly. However, if a class is compiled separately, some warnings might appear, because some external variables and functions are not visible to the class.

The question is. Is it necessary for the class itself to compile without errors, I mean, to already contain all used copies of global variables, references to objects, etc.? If so, when would be the best time to do this? If there are a lot of interacting objects, how to arrange them so that there is less confusion? Do you create diagrams of your programs if they proliferate?

Share how you write and implement work with your objects.

Global variables and stats, except for those inherent to the platform, are an evil thing. And all entities (groups of classes) must be isolated and communicate with each other according to a protocol (that of the Interface) and nothing else.

This is of course the ideal :-) In real life you make the decision and support yourself, because no one else knows your project, idea and style.

 
struct A
{
   int x,y;
};

//+------------------------------------------------------------------+
void OnStart()
{
   A a,b,c;
   a.x = 1;
   a.y = 2;
   b = a;
   c = a;
   //b = c = a;  //'operator=' - parameter passed as reference, variable expected
}

1. why doesn't it compile?

2. is there a hack to describe operator= , and use the native copy operator in it? .... i.e. some way to call::=

 
Igor Makanu:

1. why doesn't it compile?
2. is there a hack to describe operator= , and use the native copy operator in it? .... i.e. some way to call ::=

1) default assignment operator in MQL returns void data type;
2) you can do something like this:

struct A{
   int x,y;
   
   A(){}
   A(const A &a){
      this.x = a.x;
      this.y = a.y;
   }
   A operator=(const A &a){
      this.x = a.x;
      this.y = a.y;
      return a;
   }
};

void OnStart(){
   A a,b,c;
   a.x = 1;
   a.y = 2;
   b = a;
   c = a;
   b = c = a;  
}
 
Sergey Dzyublik:

1) the default assignment operator in MQL returns void data type;

do not agree:

struct A
{
   int x[];
};
//+------------------------------------------------------------------+
A myfunc(const int size)
{
   A result;
   ArrayResize(result.x, size);
   for(int i = 0; i < size; i++) result.x[i] = i + 1;
   return(result);
}
//+------------------------------------------------------------------+
void OnStart()
{
   A a;
   a = myfunc(5);
   A b;
   b = a;            // скопировали
   b.x[0] = 99;      // изменили 1-й элемент, чтобы убедиться, что это копия а, а не ссылка как в C#
   ArrayPrint(a.x);
   ArrayPrint(b.x);
}
//+------------------------------------------------------------------+

2020.04.18 18:54:03.855 tst (EURUSD,H1) 1 2 3 4 5

2020.04.18 18:54:03.855 tst (EURUSD,H1) 99 2 3 4 5


Clearly the default assignment operator returns the full data type and makes a copy, or what void are we talking about?


2. the example is good, but I want to preserve the possibility of copying in array structures by default copy operator - I checked it, in general it works quite well with dynamic arrays, there are a couple of nuances, but the question is different for now

 
Igor Makanu:

The default assignment operator clearly returns the full data type and makes a copy, or what void are we talking about?

Wrong.

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

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

struct A
{
   int x;
   int y;
   
   A operator=( const A& a )
   {
      x = a.x;
      y = a.y;
      
      return this;
   }
};

void OnStart()
{
   A a,b,c;
   a.x = 1;
   a.y = 2;
   b = a;
   c = a;
   b = c = a;
}
Reason: