I have a habit of using static variables because I think it can make the code more readable compared to only using global variables instead of static variables.
And it seems that using it in a method isn't the smartest thing to do, especially if the class is instantiated multiple times.
It's a matter of object-oriented programming
And it seems that using it in a method isn't the smartest thing to do, especially if the class is instantiated multiple times.
everything has its purpose. For some tasks, it is THE ONLY solution. For other things it is not necessary. No one is forcing you to use them :). Static methods in classes are more widely used. Static variables more rare.
One example of proper usage of static variable could be saving the environment state. Something like this:
static bool cTerminal::VisualMode(void) { //=============== static const bool visual=(bool)::MQLInfoInteger(MQL_VISUAL_MODE); //=============== //=============== return(visual); //=============== }
Since this state won't change, you only get it once at EA start and later share among all instances of the class.
The same works for a member
static const bool m_isvisual;which will be calculated only once during class construction.
everything has its purpose. For some tasks, it is THE ONLY solution. For other things it is not necessary. No one is forcing you to use them :). Static methods in classes are more widely used. Static variables more rare.
One example of proper usage of static variable could be saving the environment state. Something like this:
Since this state won't change, you only get it once at EA start and later share among all instances of the class.
The same works for a member
which will be calculated only once during class construction.This is not what is discussed.
This is what is discussed :
class C { public: void CallAMethodWithAStatic(void) { static int MyStaticCounter = 0; MyStaticCounter++; } };
Well, I thought
I gave an example of when static values in class methods can be used without problems. Why is it not what is discussed?
It's a matter of object-oriented programming
And it seems that using it in a method isn't the smartest thing to do, especially if the class is instantiated multiple times.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm told that static values in class methods should absolutely be avoided, because if the class is instantiated multiple times, the values of these static values will be shared between all instances... a real bug to find.
Therefore, the only way to do this is to re-upload this static element to a member.
Your opinion on the matter
THANKS