You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Static variable, it will be the same for all instances without inheritance.
Checked it out, well yes! it turned out to be simpler than that!
2019.08.29 15:14:09.847 tst__ EURUSD,M15: initialized
2019.08.29 15:14:09.847 tst__ EURUSD,M15: B::Bru
2019.08.29 15:14:09.847 tst__ EURUSD,M15: B::Bru
2019.08.29 15:14:09.847 tst__ EURUSD,M15: B::Bru
2019.08.29 15:14:09.847 tst__ EURUSD,M15: B::Bru
2019.08.29 15:14:09.847 tst__ EURUSD,M15: B::Bru
2019.08.29 15:14:09.847 tst__ EURUSD,M15: MyTerminalInfoString
I see that the Language variable has only been initialized once, i.e. this is a simpler solution
Thank you!
Please tell me, how is it different?
Creating an object, or pointer, in this way
from classic creation
The difference is that in the first case, you don't have to specify theCClass class identifier in C++.
I sketched out my class, which should initialise fields once with constant values, and it seems to work as intended:
I don't like 2 things:
1. I call SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP) - because order of initialization is not defined, i.e. it's not sure that VolumeSTEP will be initialized first, and only thenGetDigitsInVolumeStep() will be called
2. I want to get rid of static method static int GetDigitsInVolumeStep() - I've seen a video on youtube saying that in pure OOP one should not use static methods, and now I'm fighting against windmills
the link to the video, it is essentially the samehttps://youtu.be/lfdAwl3-X_c andhttps://youtu.be/zME4SOCHT0I
how can i rewrite these 2 points that i don't like differently ?
1. I repeat the call to SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP) - because the order of initialisation is not specified, i.e. it is not certain that I will have VolumeSTEP initialised first, and only then GetDigitsInVolumeStep() will be called
1. i.e. are you afraid that the order of statics initialization is undefined? Actually it's not, the pluses initialize in order of definition in the code. So don't sweat it, VolumeSTEP first and then VolumeDIGITS.
1. So you are afraid that order of statics initialization is undefined? Actually it's not so, in pluses initialization is in order of definition in code. So don't sweat it, first VolumeSTEP and then VolumeDIGITS.
Yes, not that I'm afraid, let's say I'm protected, I believe that the code should have at least some guarantee against changes in compilation behavior - in general YES
ZS: here in general, why the topic for the first time a day - I want to see how realistic to make something like in the video author Egor says, while I doubt that we get something effective - now I'm using static methods, and with the initialization are already some doubts appeared
1. i.e. are you afraid that the order of initialisation of statics is undefined? Actually it's not, the pluses initialize in order of definition in the code. So don't sweat it, VolumeSTEP first and then VolumeDIGITS.
In MQL too.
https://www.mql5.com/ru/docs/basis/oop/staticmembers
Yes, not that I'm afraid, let's say I'm protected, I believe that the code should have at least some guarantee against changes in compilation behavior - in general YES
ZS: here in general, why the topic for the first time a day - I want to see how realistic to make something like in the video author Egor says, while I doubt that we will get something effective - now I'm using static methods, and with the initialization are already some doubts appeared
To use it this way:
And in code like this:
Use as follows:
And in the code like this:
oops, a little bit of a misdirect, that's what I can do
Here's what I want to achieve in general:
1. the CDeal class is standalone, it opens/closes/controls its order by one strategy - the strategies are enumerated, so the optimizer can scroll through the mix of strategies
2. according to step 1, I do not need anywhere else to have the terminal environment variables SYMBOL_VOLUME_MAX, SYMBOL_VOLUME_MIN,SYMBOL_VOLUME_STEP and quantity of characters in SYMBOL_VOLUME_STEP - it does not change during MQL-program operation
3. starting from items 1 and 2, i can encapsulate methods of opening/closing/trading an order and order properties themselves in one class CDeal - i'm not going to use all that anywhere later, am i? - i.e. i can use all of them in one class
pp 1-3 everything is solvable, but... OK, now you have helped me with the statics, let it be so, because there is a help, at least one way to justify my decision, now the code is like this
created 3 instances of Cdeal, got it in the log:
2019.08.29 21:53:53.613 AnyGrid_v1.00 EURUSD,M30: initialized
2019.08.29 21:53:53.613 AnyGrid_v1.00 EURUSD,M30: Cdeal::CdealVolumeMAX = 100000.0 , VolumeMIN = 0.01 , VolumeSTEP = 0.01 , VolumeDIGITS = 2
2019.08.29 21:53:53.613 AnyGrid_v1.00 EURUSD,M30: Cdeal::CdealVolumeMAX = 100000.0 , VolumeMIN = 0.01 , VolumeSTEP = 0.01 , VolumeDIGITS = 2
2019.08.29 21:53:53.613 AnyGrid_v1.00 EURUSD,M30: Cdeal::CdealVolumeMAX = 100000.0 , VolumeMIN = 0.01 , VolumeSTEP = 0.01 , VolumeDIGITS = 2
2019.08.29 21:53:53.613 AnyGrid_v1.00 EURUSD,M30: Cdeal::GetDigitsInVolumeStep
So far everything has worked as planned!
I.e., now there are no sections of repeated code (calls) - it is an efficient code, but how to get away from using static function static int Cdeal::GetDigitsInVolumeStep() - in the video, the author believes that it is possible to write OOP code without static functions, I see that not only is it inconvenient, but it is not always possible to use everything, about Get and Set methods - most likely I will do without them, I'll see later
The static field option has one huge disadvantage. You can't have different values of this field in different instances of the class.
Yes, thanks, I know that, the purpose is just to get an efficient code without unnecessary variables and unnecessary use of memory
I still have not solved the question:how to avoid using static function static int Cdeal::GetDigitsInVolumeStep()
Here's an outline of my class, which should initialize fields once with constant values, it seems to work as intended: