1. new operator returns a pointer, of course, developers made a mess with implicit dereferencing, so your version works, but it's better not to get hung up on undocumented things.
2. Of course, we don't have C++, but it's very similar, so initialization lists (I don't know about efficiency) are kosher.
Checked, yes your option works, and logically it's more correct:
The pointer and the object created at initialization were not lost, and the method in the interface did not substitute the methods of the classes 1 and 2 - everything seems to work as planned
How can I avoid repetitive code sections during initialisation? - in the constructor, you can't
Instead of interface you make an ordinary class. You put *m there and make a bool SomeMethod {if(CheckPointer(m)) return false; m=new...; return true;}
Vladimir Simakov: Instead of interface, you make a regular class. There you put *m and make method bool SomeMethod {if (CheckPointer(m)) return false; m=new...; return true;}
In Algoritm if (SomeMethod()) ...
No, I need the interfes - it's very cool that I won't need to pull everything at every tick - I'll make one call of one strategy per one tick - in general I like config of all this so far, it seems it was necessary to solve my problem this way:
Vladimir Simakov: No, that won't work either. If you declare an abstraction, be kind enough to implement it.
I think the compiler perceives the Algorithm() method from the interface and the declared virtual void Algorithm() as different methods because of the virtual modifier
Vladimir Simakov: An abstract method must be implemented in descendants. Once again, there's no need to implement it.
I need interfaces because it is convenient for me to declare different behaviour for 6 strategies depending on Expert Advisor settings (there are not so many strategies but order systems)
right now:
class CGrid {
private:
int StrategyCount;
IStrategy *Strategy[];
public:
CGrid(int eamagicnumber, SSettingsForOrder &settingstrategy[]);
~CGrid(); };
//+------------------------------------------------------------------+void CGrid::CGrid(int eamagicnumber, SSettingsForOrder &settingstrategy[]) {
StrategyCount = ArraySize(settingstrategy);
ArrayResize(Strategy, StrategyCount);
for(int i = 0; i < StrategyCount; i++) {
settingstrategy[i].magicnumber = i+eamagicnumber;
switch(settingstrategy[i].dealtype) {
case NONE : Strategy[i] = new CNONE(settingstrategy[i]); break;
case BUY : Strategy[i] = new CBUY(settingstrategy[i]); break;
case ReOpenBUY : Strategy[i] = new CReOpenBUY(settingstrategy[i]); break;
case ReversBUYToSellOnly : Strategy[i] = new CReversBUYToSellOnly(settingstrategy[i]); break;
case ReversBUYAlwaysRevers : Strategy[i] = new CReversBUYAlwaysRevers(settingstrategy[i]); break;
case SELL : Strategy[i] = new CSELL(settingstrategy[i]); break;
case ReOpenSELL : Strategy[i] = new CReOpenSELL(settingstrategy[i]); break;
case ReversSELLToBuyOnly : Strategy[i] = new CReversSELLToBuyOnly(settingstrategy[i]); break;
case ReversSELLAlwaysRevers: Strategy[i] = new CReversSELLAlwaysRevers(settingstrategy[i]); break; } } }
I want to minimize the body of Algorithm() methods - if I write the base class correctly - in my example, class CStrategy, then the Algorithm() itself will contain 5-10 lines at most
How should I write this code without an interface? - Now everything is brief and there will only be left to pull the algorithms themselves strategies every tick or less frequently
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
1. new operator returns a pointer, of course, developers made a mess with implicit dereferencing, so your version works, but it's better not to get hung up on undocumented things.
2. Of course, we don't have C++, but it's very similar, so initialization lists (I don't know about efficiency) are kosher.
Checked, yes your option works, and logically it's more correct:
2019.08.31 22:07:35.196 tst (EURUSD,H1) Strategy_1::Strategy_1
2019.08.31 22:07:35.196 tst (EURUSD,H1) Context::Context
2019.08.31 22:07:35.196 tst (EURUSD,H1) Strategy_2::Strategy_2
2019.08.31 22:07:35.196 tst (EURUSD,H1) Context::Context
2019.08.31 22:07:35.196 tst (EURUSD,H1) Strategy_1::Algorithm
2019.08.31 22:07:35.196 tst (EURUSD,H1) Strategy_2::Algorithm
2019.08.31 22:07:35.197 tst (EURUSD,H1) Strategy_1::Algorithm
The pointer and the object created at initialization were not lost, and the method in the interface did not substitute the methods of the classes 1 and 2 - everything seems to work as planned
How can I avoid repetitive code sections during initialisation? - in the constructor you cannot
How can I avoid repetitive code sections during initialisation? - in the constructor, you can't
Instead of interface, you make a regular class. There you put *m and make method bool SomeMethod {if (CheckPointer(m)) return false; m=new...; return true;}
No, I need the interfes - it's very cool that I won't need to pull everything at every tick - I'll make one call of one strategy per one tick - in general I like config of all this so far, it seems it was necessary to solve my problem this way:
I tweaked my example a bit - it works, but it should look like this:
but if I remove the body of theCStrategy::Algorithm(void) method, there will be an error: 'Algorithm' -the function must have a body
Why is it an error? - because I declare the CStrategy::Algorithm(void) method with the modifier virtual
I tweaked my example a bit - it works, but it should look like this:
but if I remove the body of the CStrategy::Algorithm(void) method, there will be an error: 'Algorithm' - the function must have a body
Why is it an error? - because I declare the CStrategy::Algorithm(void) method with the modifier virtual
virtual void Algoritm()=0;
No, that won't work either. If you declare an abstraction, be kind enough to implement it.
I think the compiler perceives the Algorithm() method from the interface and the declared virtual void Algorithm() as different methods because of the virtual modifier
An abstract method must be implemented in descendants. Once again, there's no need to implement it.
I need interfaces because it is convenient for me to declare different behaviour for 6 strategies depending on Expert Advisor settings (there are not so many strategies but order systems)
right now:
I want to minimize the body of Algorithm() methods - if I write the base class correctly - in my example, class CStrategy, then the Algorithm() itself will contain 5-10 lines at most
How should I write this code without an interface? - Now everything is brief and there will only be left to pull the algorithms themselves strategies every tick or less frequently
как этот код без интерфейса написать? - сейчас все кратко и останется только дергать сами алгоритмы стратегий каждый тик или реже