Questions on OOP in MQL5 - page 36

 
Igor Makanu:

Well yes, that's a better solution! so that there is one less method

There should only be one new in OnStart.
 
Koldun Zloy:

1. There is nothing wrong with calling this function twice.

2. Don't you mind that those who are against using static functions don't give the slightest argument?

You'd better not watch videos from just anybody but read books.

I wish the author of the videos https://youtu.be/lfdAwl3-X_c and https://youtu.be/zME4SOCHT0I many good things..... knocked out the whole week, everything I sit down to do I start thinking, not doing as before .... you cannot use statics, it's not OOP!

I want to finish the code, I have a question:

- I want to save fields of objects, good thing, the fields were immediately placed in the structures, and you can save them with a single click via FileWriteStruct()

- there are N objects of one class, all of them inherited from one base class

- because i have watched the video, i was hoping that some method would help me not to interrogate each object if it needed to save the file.... In general, fiasco, it was decided not to be foolish and create a method that will statically assign a flag to write to the file

class CStrategy: public IStrategy
{
protected:
static bool       f_save_required;
void              SetSaveRequired()        { CStrategy::f_save_required = true;                    }
public:
bool              GetSaveRequired()        { return(CStrategy::f_save_required);                   }

It works as planned

And the question itself, what will happen if you write it this way:

class CStrategy: public IStrategy
{
private:
static bool       f_save_required;
protected:
void              SetSaveRequired()        { CStrategy::f_save_required = true;                    }
public:
bool              GetSaveRequired()        { return(CStrategy::f_save_required);                   }

what will happen if you write it this way:

class CStrategy: public IStrategy
{
private:
static bool       f_save_required;
protected:
void              SetSaveRequired()        { f_save_required = true                   }
public:
bool              GetSaveRequired()        { return(f_save_required);                   }
 

tried different ways to modify the test script, I can't see any difference, show me the correct modifiers for the post above: (protected / private ) and call M::x or just x

//+------------------------------------------------------------------+
interface I
{  bool main();

};
//+------------------------------------------------------------------+
class M:public I
{
protected:
static int        x;
bool              main()   { x++;  Print(__FUNCTION__," = ",x); return(x>0); }
public:
                     M()      { Print(__FUNCTION__);}
                    ~M()      { Print(__FUNCTION__);}
};
//+------------------------------------------------------------------+

static int M::x=0;
//+------------------------------------------------------------------+
class A:public M
{
public:
                     A()      { Print(__FUNCTION__);}
                    ~A()      { Print(__FUNCTION__);} };
//+------------------------------------------------------------------+
class B:public M
{
public:
                     B()      { Print(__FUNCTION__);}
                    ~B()      { Print(__FUNCTION__);} };
//+------------------------------------------------------------------+
class C:public M
{
public:
                     C()      { Print(__FUNCTION__);}
                    ~C()      { Print(__FUNCTION__);} };
//+------------------------------------------------------------------+

void OnStart()
{  I *a = new A;
   I *b = new B;
   I *c = new C;
   I *m = new M;

   a.main();
   b.main();
   c.main();
   m.main();


   delete a;
   delete b;
   delete c;
   delete m;

}
//+------------------------------------------------------------------+
 
Igor Makanu:

tried different ways to modify the test script, I see no difference, show me the correct modifiers for the post above: (protected / private ) and call M::x or just x

Inside class methods you call x, in all other places M::x. If protected, then the reference is possible only in methods of the class in which the field is defined, here the logic is common.
 
Happy holiday, by the way. Another byte has happened.
 
Vladimir Simakov:
Inside class methods you call x, in all other places M::x, if protected, then the access is possible only in methods of the class in which the field is defined, here the logic is common.

Right! I made a wrong example in my sleep, so I got 'M::main' - cannot access private member function:

class M
{
private:
   static int        x;
   bool              main()   { x++;  Print(__FUNCTION__," = ",x); return(x>0); }
public:
                     M()      { Print(__FUNCTION__);}
                    ~M()      { Print(__FUNCTION__);} };
//+------------------------------------------------------------------+

static int M::x=0;
//+------------------------------------------------------------------+
class A:public M
{
public:
                     A()      { Print(__FUNCTION__);}
                    ~A()      { Print(__FUNCTION__);}
   void                inc()     {main();} };
//+------------------------------------------------------------------+
class B:public M
{
public:
                     B()      { Print(__FUNCTION__);}
                    ~B()      { Print(__FUNCTION__);}
   void                inc()     {main();} };
//+------------------------------------------------------------------+
class C:public M
{
public:
                     C()      { Print(__FUNCTION__);}
                    ~C()      { Print(__FUNCTION__);}
   void                inc()     {main();} };
//+------------------------------------------------------------------+
void OnStart()
{  A *a = new A;
   B *b = new B;
   C *c = new C;
   M *m = new M;

   a.inc();



   delete a;
   delete b;
   delete c;
   delete m;

}
//+------------------------------------------------------------------+

got 'M::main' - cannot access private member function tst.mq5 25 32

If I write it down

protected:   
   bool              main()   { x++;  Print(__FUNCTION__," = ",x); return(x>0); }

everything works as planned!

 
Vladimir Simakov:
Happy holiday, by the way. Another byte happened.

Thank you, it's sorted!

Happy Holidays!

 
Igor Makanu:

I wish the author of the videos https://youtu.be/lfdAwl3-X_c and https://youtu.be/zME4SOCHT0I a lot of good things..... knocked out all week, everything I sit down to do I start thinking rather than doing as before .... you cannot use statics, it's not OOP!

I want to finish the code, I have a question:

- I want to save fields of objects, good thing, the fields were immediately placed in the structures, and you can save them with a single click via FileWriteStruct()

- there are N objects of one class, all of them inherited from one base class

- because i have watched the video, i was hoping that some method would allow me to avoid asking each object if it should save the file.... In general, fiasco, it was decided not to be foolish and create a method that will statically assign a flag to write to the file

It works as planned

And the question itself, what will happen if you write it this way:

what happens if you write it this way:

What does it depend on whether an object needs to be saved to a file?

Is this flag common for all objects?

 
Koldun Zloy:

What does it depend on to save the object to a file?

Is this a feature common to all objects?

Well the question is dialectical, of course, here

- It is better to have 10 strategies and write 10 files with change of state of TC (1 TC = 1 structure)

- or it is more logical to pass one handle from the class, where there is a call ( search) of strategies per tick, so they all write to one file

I think the 2nd variant is more logical


it turns out that after the start of all TCs you need to write 10 structures once - the state has changed

then in random order they need to write when needed ( triggering of SL or TP) - here it is also faster to open the file and write 10 structures in one go, strategies are not pipswise, not every second, from a minute to an hour in each TS


So far I have implemented a description of a static variable and access to it through a get - set, set protected, get public - it also resets to false the static variable after the call

 
Igor Makanu:

Well, the question is dialectical, of course, here

- You have either 10 strategies and write 10 files with change of state of TC (1 TC = 1 structure)

- or it is more logical to pass one handle from the class, where there is a call ( search) of strategies on a tick, so they write all into one file

I think the 2nd variant is more logical


it turns out that after the start of all TCs you need to write 10 structures once - the state has changed

then in random order they need to write when needed ( triggering of SL or TP) - here it is also faster to open the file and write 10 structures in one go, strategies are not pipswise, not every second, from a minute to an hour in each TS


For now I have implemented by describing a static variable access to it through a Get, Set Protected, Get public - it also resets to false the static variable after it has been called

This is all clear. It is not clear whySetSaveRequired() andGetSaveRequired() are non-static but write to a static variable.