Is it possible to implement a singleton pattern in MQL4. - page 2

 
But for some reason there are a lot of errors at compile time. What's wrong?

What paternas can we talk about if you don't know the basics, you couldn't even create a static class field correctly.

(there are a lot of articles about singleton on hobber, what it's for, how to do it and what's wrong with it).

Singleton or a static class?
Using the singleton pattern
 
ALXIMIKS:

(there are lots of articles about singleton on the hubrabs and what it's for, and how, and what's wrong with it)

Singleton or static class?
Using Singleton pattern.

Do you think I haven't encountered this? I haven't quite figured out how it works. Here's the thing. But as it turns out, I don't need a singleton. So I'll make do with static members.

ALXIMIKS:

How can we talk about paternals if you don't know the basics, you couldn't even create a static class field correctly.

If you know how, you may correct it. Yesterday I wrote according to documentation. But there are a lot of errors too. Like this:

struct Symbol_Properties
{
   static datetime    gdt_Quote;           // Время поступления последней котировки
   static double      gda_Price [2];       // Текущие рыночные цены (0 - Bid, 1- Ask)
   static double      gd_Spread;           // Размер спреда в пунктах
   static double      gd_Swap;             // Своп
   static double      gd_Comission;        // Комиссия
   static double      gd_Pt;               // Величина одного пункта
   static int         gi_Digits;           // Количество знаков в цене после запятой
   static int         gi_StopLevel;        // Минимально-допустимый уровень стоп-лосса/тейк-профита в пунктах
   static int         gi_FreezLevel;       // Уровень заморозки ордеров в пунктах
};
int Symbol_Properties::gdt_Quote = 0;
int Symbol_Properties::gda_Price = 0;
int Symbol_Properties::gd_Spread = 0;
int Symbol_Properties::gd_Swap = 0;
int Symbol_Properties::gd_Comission = 0;
int Symbol_Properties::gd_Pt = 0;
int Symbol_Properties::gi_Digits = 0;
int Symbol_Properties::gi_StopLevel = 0;
int Symbol_Properties::gi_FreezLevel = 0;

What's next?

 
static double       gd_Spread; 
int  Symbol_Properties::gd_Spread = 0;
not sad about anything?
 
Естественно, создавать несколько объектов в разных классах данных структур крайне не рекомендуется.
Tell me what this is all about. And translate the above quote more clearly, alas I don't understand anything, thank you.
 

I threw the wrong one off. Anyway, the current version is correct here:

struct Symbol_Properties
{
   static datetime    gdt_Quote;           // Время поступления последней котировки
   static double      gda_Price [2];       // Текущие рыночные цены (0 - Bid, 1- Ask)
   static double      gd_Spread;           // Размер спреда в пунктах
   static double      gd_Swap;             // Своп
   static double      gd_Comission;        // Комиссия
   static double      gd_Pt;               // Величина одного пункта
   static int         gi_Digits;           // Количество знаков в цене после запятой
   static int         gi_StopLevel;        // Минимально-допустимый уровень стоп-лосса/тейк-профита в пунктах
   static int         gi_FreezLevel;       // Уровень заморозки ордеров в пунктах
};

datetime   Symbol_Properties::gdt_Quote = 0;
double     Symbol_Properties::gda_Price [2] = {0.0, 0.0};
double     Symbol_Properties::gd_Spread = 0;
double     Symbol_Properties::gd_Swap = 0;
double     Symbol_Properties::gd_Comission = 0;
double     Symbol_Properties::gd_Pt = 0;
int        Symbol_Properties::gi_Digits = 0;
int        Symbol_Properties::gi_StopLevel = 0;
int        Symbol_Properties::gi_FreezLevel = 0;

I guess now I have to refer to each static variable by such a long name?

Symbol_Properties::gd_Spread = 0;

Or is it easier to implement it so that after description of variable initialization structure when it' s initialized the corresponding static member should be assigned to some variable of type:

double Spread = Symbol_Properties::gd_Spread = 0;

And then in the code refer to the corresponding static variables by this variable, right?

 
hoz:

Yeah, well... The main thing is thatVadim knows :)))))

Yeah, that's the normal kind of dialogue:

Q: My friends recommended some candy to me. It's just what I need!

Me: (Puzzled... What does this have to do with candy? Maybe B is going to a friend's birthday party or wants to treat the kids, maybe his own or someone else's? Maybe he went into business and is now selling candy. Maybe it was the last candy in Belarus and B is now a monopolist. What if B missed the candy? A lot of other thoughts ran through my head on the subject of "why candy and what to do with it". Once again, as before with B, my telepathic abilities failed me. Nothing came to mind).

Not a clue.

Just in case. It's rude to post dialogues without the consent of everyone involved in the conversation. It's mean-spirited.
 

1. What is all this for?

2. There are two ways to access the static fields of a class (the structure is a public access class by default and inheritance):

(a) Through the class namespace - e.g.Symbol_Properties::gd_Spread

(double Spread = Symbol_Properties::gd_Spread) - theSpread value becomes equal togd_Spread from theSymbol_Propertiesclass

(double Spread = Symbol_Properties::gd_Spread =0) value ofgd_Spread fromSymbol_Properties class andSpreadvalue becomes equal to 0

b) create an object of class (for example,Symbol_Properties obj; ) and refer to it as a usual field of the class through this object

(double Spread = obj.gd_Spread)

(double Spread = obj.gd_Spread =0)

 
ALXIMIKS:

1. What's it all for?

Convenience... After all, if these variables are used in a single instance, why should I create an object? Besides, it's much more convenient to read the code when referring to a variable, if VARIABLE.VARIABLE NAME.

ALXIMIKS:

2. There are two ways of accessing class static fields (structure is a class with public access by default and inheritance)

(a) via the class namespace - e.g.Symbol_Properties::gd_Spread

(double Spread = Symbol_Properties::gd_Spread) - theSpread value becomes equal togd_Spread from theSymbol_Propertiesclass

(double Spread = Symbol_Properties::gd_Spread =0)- thegd_Spread value from classSymbol_Properties and theSpreadvalue becomes equal to 0

Exactly! That's why I did it that way. I immediately initialize the variable which refers to the variable of this structure by zero and then it's stored in memory permanently. Which is logical, because these variables are needed in a single instance. That's why there's no reason to create different object in this case. After all, it is logical... Don't you agree with me?

ALXIMIKS:

b) Create a class object (e.g.Symbol_Properties obj; ) and refer to it as a normal class field through this object

Then the variables will not be static, and there is no guarantee that they will not retain their values as the program runs.
 
What's wrong with STATIC VARIABLE, or constants for example?
 

STATE VARIABLE did not please me, because they are used in different classes. So I sort of grouped them together.

Constants are not liked because constants do not change their values, while these variables should be able to change their values.