Questions on OOP in MQL5 - page 38

 
Nikolai Semko:

Yes, I too have a similar situation. It's not hard to learn, it's harder to retrain, to break old habits. I still can't kick many bad habits of procedural programming.

relearn from procedural style? hmm, well written procedural style code is usually easy to wrap into a class, if you need to scale up the project

Dmitry Fedoseev:

You can just forget about constants and statics for now. And don't even think about interfaces.

And when everything is ready, then look and mark something as a statics and something as a constant. And you can forget about interfaces altogether.

I know how to do a lot of things fast,

I decided to devote my time to studying theory, or rather to bring MQL up to the level of what has been out there in the past few years, even many people here don't know about the capabilities of terminals

If I'm going to study the material, I need to study it properly, imho, and to understand whether MQL is closer to C++ or C# - for now it seems to be C++. In C# people don't even bother with modifiers, or rather they just fix whatever the studio marks as suspicious )))

 
Dmitry Fedoseev:

Constants and statics can simply be ignored for the time being. Interfaces, too.

And when everything is ready, then look and mark something as a static and something as a constant. And you can forget about interfaces altogether.

The main thing is to hold constants and statics. I agree about the interfaces.

 
Igor Makanu:

retrain from procedural style? hmm, well written procedural style code wrapped in a class, if you need to scale up the project, usually no problem

I know how to do a lot and fast,

I've decided to devote my time to studying theory, or rather to bring MQL up to the level of what's been developed over the last few years, even many people here are not up to date on the capabilities of terminals

And if you study the material you have to study it properly, imho, and to understand what is closer to MQL to C++ or to C# all the same - for now it seems to be C++.

const - if it is necessary to forbid assignment of a variable (except for one-time initialization). If a variable is declared as conts, then when you pass it into a function by reference, function argument should also be const, but the compiler will make this happen, you don't have to think about it. If a variable is not being assigned, it can also be marked as const - it will work faster, this applies to function arguments. However, at any moment, you may need to modify it...

static - if it's a variable in a class, it's a rare case, even the rarest. If it is a class method, then if the method handles only arguments of this method andstatic variables of the class, it is also a rare case (however, not so rare, if you just collect functions into a class for convenience).

 
Asleep?
 
Igor Makanu:

I'd like an example, I'm telling you, I'm not good with modifiers at all.


SZS: in the meantime what goes on in general horror in programming discussion in Inet - here was an article about const on hubra last month, the meaning - yes, this const is not necessary, look assembler code does not differ that without const - the compiler will remove everything ((

Here is an example:

struct SSettingsForOrder
{
   int mPeriod;
   double mTakeProfit;
   double mStopLoss;
};

class CStrategy
{
   SSettingsForOrder mSettings;
   
   bool mDirty;
   
public:
   CStrategy() : mDirty( true ){}

   CStrategy( SSettingsForOrder& set ) : mSettings( set ), mDirty( true ){}
   
   virtual void NextStepStrategy();
   
   bool IsDirty() const
   {
      return mDirty;
   }
   
   int GetPeriod() const
   {
      return mSettings.mPeriod;
   }
   void SetPeriod( int period )
   {
      if( mSettings.mPeriod != period )
      {
         mSettings.mPeriod = period;
         mDirty = true;
      }
   }
   double GetTakeProfit() const
   {
      return mSettings.mTakeProfit;
   }
   void SetTakeProfit( double takeProfit )
   {
      mSettings.mTakeProfit = takeProfit;
      mDirty = true;
   }
   double GetStopLoss() const
   {
      return mSettings.mStopLoss;
   }
   void SetStopLoss( double stopLoss )
   {
      mSettings.mStopLoss = stopLoss;
      mDirty = true;
   }
   void Load( int fileHandle )
   {
      FileReadStruct( fileHandle, mSettings );
      mDirty = false;
   }
   void Save( int fileHandle )
   {
      FileWriteStruct( fileHandle, mSettings );
      mDirty = false;
   }
};

class CStrategy_XXX : public CStrategy
{
public:
   CStrategy_XXX(){}
   CStrategy_XXX( SSettingsForOrder& set ) : CStrategy( set ){}
   
   virtual void NextStepStrategy();
};

class CStrategy_YYY : public CStrategy
{
public:
   CStrategy_YYY(){}
   CStrategy_YYY( SSettingsForOrder& set ) : CStrategy( set ){}
   
   virtual void NextStepStrategy();
};

class CStrategy_ZZZ : public CStrategy
{
public:
   CStrategy_ZZZ(){}
   CStrategy_ZZZ( SSettingsForOrder& set ) : CStrategy( set ){}
   
   virtual void NextStepStrategy();
};

CStrategy* Strategies[3];

void OnInit()
{
   Strategies[0] = new CStrategy_XXX();
   Strategies[1] = new CStrategy_YYY();
   Strategies[2] = new CStrategy_ZZZ();
   
   int fileHandle = FileOpen( ... );
   for( int i = 0; i < 3; i++ )
   {
      Strategies[i].Load( fileHandle );
   }
   FileClose( fileHandle );
}

void TestAndSave()
{
   bool saveRequired = false;
   for( int i = 0; i < 3; i++ )
   {
      if( Strategies[i].IsDirty() )
      {
         saveRequired = true;
         break;
      }
   }
   if( saveRequired )
   {
      int fileHandle = FileOpen( ... );
      for( int i = 0; i < 3; i++ )
      {
         Strategies[i].Save( fileHandle );
      }
      FileClose( fileHandle );
   }
}

void OnDeinit()
{
   TestAndSave();
   
   for( int i = 0; i < 3; i++ )
   {
      delete Strategies[i];
   }
}

It's not clear from your code why an interface is needed, so I threw it out.

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

 
Igor Makanu:

relearning procedural style? hmm, well written procedural style code wrapped in a class, if you need to scale up the project, usually not a problem

I know how to do a lot and fast,

I've decided to devote my time to studying theory, or rather to bring MQL up to the level of what's been developed over the last few years, even many people here are not up to date on the capabilities of terminals

If I'm going to study the material, I need to study it properly, imho, and to understand what is closer to MQL to C++ or to C# all the same - for now it seems to be C++.

mql itself is based on c++, but through the efforts of its creators they implemented many bad things from Sharp, without adding anything good from it. The Sharpe-like standard library is telling. It would look more logical if it was similar to std. IMHO.
 
Koldun Zloy:

Here's an example:

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

I don't know your task completely, but it's 99.99% sure you don't need it.

this is the 5th time I've heard that you should throw out the interfaces and don't bother)))

I took the "Strategy" OOP design template as a basis.

applied, as specified, interfaces as an abstraction from which I inherit the base class CStrategy - it turned out that my CStrategy will be absent public methods, all public are described by interfaces (3 pieces = strategy itself, strategy completion and now added a record of the strategy state ) The role of the interfaces is... well, about the same as with const modifiers - you get warnings at the beginning if you don't describe a method or try to close them with inheritance - so far I have more convenience than problems.

In general I confirm that with 100% probability it's unnecessary... but it's just convenient that the CStrategy class doesn't have a public section

Your code looks good, I'll reproduce it in my place after lunch, try to compare it with mine

Thanks!

 
Vladimir Simakov:
The mql itself is based on c++, but through the efforts of its creators they've put much of the bad of sharp into it, without adding anything good of it. Sharpe-like standard library is telling. It would look more logical if it was similar to std. IMHO.

maybe it was me who started the phrase on the MQL forum "it's not C++, it's MQL..." now a lot of answers to questions like yours in this context ))))

in general the language is quite democratic, not convenient to write in MQL - "in 2 clicks" you can go to .dll C++ and here since the beginning of the year in .dll C#, if memory serves, one of the first articles on dll from Developer Renat - i.e. this possibility initially, as a product concept is offered, imho

SZS: to write above, for some reason came to mind the phrase - I am an artist - I see so!)))

 
Igor Makanu:


ZS: to the above, for some reason came to mind the phrase - I'm an artist - I see so! )))

this comes to mind very often in responses from developers, good or bad, I do not know

If the developers would give information and close questions once and for all - why you can't add or change a function

 
Fast235:

this comes up very often in the developers' responses, good or bad, I don't know

if developers would give information and close the questions once and for all - why it is impossible to add or change a function

Well this is the existing corporate style of communication between users and support of IT monopolists, the Internet is full of cases where users find the obvious bugs Microsoft and after appealing to Microsoft get in response ... usually silence ))))

ZS: from a recent read, Hubr " Why does Windows read one file a hundred thousand times to open a menu? "

Почему для открытия меню Windows читает один файл сто тысяч раз?
Почему для открытия меню Windows читает один файл сто тысяч раз?
  • habr.com
«Проводник тратит 700 мс на то, чтобы открыть контекстное меню панели задач. 75% этого времени он выполняет 114 801 операцию считывания из одного файла, средний объём считываемых данных 68 байт. Мне стоит написать пост об этом, или достаточно саркастичного твита?» За компьютером я работаю быстро, и поэтому меня раздражает, когда приходится...