Questions on OOP in MQL5 - page 7

 
Dmitry Fedoseev:

If the number of objects is known beforehand and it is constant during the running of the program, then new is not needed. In all other cases, you need new.

No, here is my examplehttps://www.mql5.com/ru/forum/160683/page861#comment_11840254

it's convenient to pass parameters to the constructor and if the user changes the settings, it's faster to kill the class in OnDeinit() and then create it with the new parameters in OnInit()

;)

 
Igor Makanu:

no, here is my examplehttps://www.mql5.com/ru/forum/160683/page861#comment_11840254

it is convenient to pass parameters to the constructor and if the user changes the settings, it's faster to kill the class in OnDeinit() and then create it with the new parameters in OnInit()

;)

Parameters can also be passed to the constructor without new.

 
Dmitry Fedoseev:

Parameters can be passed to the constructor without new.

So, how will you change class fields (user changed EA settings)? - Will you write one more method? I thought on the last page you were fighting for"one morevariable for a pointer."and here you have a whole method!

;)

 
Igor Makanu:

and? and how will you change class fields (user changed EA settings)? - Will you write another method? I thought on the last page you were fighting for"one morevariable for the pointer." you were struggling with, and here's a whole method!

;)

input int a1=1;
input int a2=2;

class CX{
   public:
   void CX(int a,int b){
   
   }
};

CX cx(a1,a2);
 
Dmitry Fedoseev:

no way ;)

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input int a1=1;
input int a2=2;
//+------------------------------------------------------------------+
class CX
  {
public:
   int a1,a2;
   void CX(int a,int b) {a1=a;a2=b; }
  };
CX cx(a1,a2);  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
void OnTick()
  {
      Comment("cx.a1 = ",cx.a1,"\ncx.a2 = ",cx.a2);
  }
//+------------------------------------------------------------------+

change the EA settings

 
Igor Makanu:

no way ;)

change EA settings

Cool ambush.

However, I would prefer to add a method to change the parameters, but not use new just because of the parameters.
 
Dmitry Fedoseev:

Cool ambush.

Nevertheless, I prefer to add a method to change parameters, but don't use new just because of parameters.

not using new is a superstition? )))

imho, if it is convenient, you must use it! - Your example will be rewritten in 2 clicks using new and everything will work correctly and handle situation when user changes settings

#property strict
input int a1=1;
input int a2=2;
//+------------------------------------------------------------------+
class CX
  {
public:
   int a1,a2;
   void CX(int a,int b) {a1=a;a2=b; }
  };
CX *cx;
//+------------------------------------------------------------------+
int OnInit()
  {
   cx = new CX(a1,a2);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  delete cx;
  }
//+------------------------------------------------------------------+
void OnTick()
  {
      Comment("cx.a1 = ",cx.a1,"\ncx.a2 = ",cx.a2);
  }
//+------------------------------------------------------------------+
 
Igor Makanu:

not using new is superstition? )))

imho, if it is convenient, you must use it! - Your example will rewrite in 2 clicks using new and everything will work correctly and handle situation when user changes settings

Not superstition, just laziness, historically, due to circumstances. You have to write delete and do it in Deinit(). But the Deinit() function was not present in the template by default. Now I look - the EA template has Deinit(), but it was not there before.

 
Dmitry Fedoseev:

Not superstition, just laziness, historically, due to circumstances. We should write delete, and do it in Deinit(). But the Deinit() function was not present in the template by default. I am looking now - there is Deinit() in the EA template, but it was not there before.

Do not write delete - everything will work correctly, this sin (I mean superstition) ) ) will take over the terminal and will mutter in the log "48 bytes of leaked memory", "2 objects of type CX left" and "undeleted objects left".

HH: in the indicator template there is no Deinit() - this is annoying

 
Igor Makanu:

do not write delete - everything will work correctly, this sin (I mean superstition)) ) will take over the terminal and will mutter in its log "48 bytes of leaked memory", then "2 objects of type CX left" and "undeleted objects left"

SZY: in a template of creation of the indicator there is no Deinit() - it strains

It will work without delete, but it's useless. But does the terminal take care of this problem? It only reports memory leaks, but it doesn't devote the same objects.