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

 
sergeev:

What's this?

That's what you asked for. An instance of a class... with an extern modifier. Absurd, of course. But...

That's what you said. But I haven't found any need for it.

sergeev:

But wouldn't it be a good idea to make an instance of a structure extern?

I don't understand what extern has to do with it at all? I don't need to "make" some data to be shared, so they can be included into any other classes. And these variables must not lose their values during program execution, i.e. they must be static. You're telling me about extern. What for?

 
hoz:

This is what you asked for. A class instance... with an extern modifier. Absurd, of course. But...

did it compile at all?


I don't need to "make" some data to be shared,
like you need?
 
sergeev:

did it compile at all?

I haven't checked. As what is needed has not yet been implemented.

sergeev:

What do you need?

I'll repeat what I need... There is a structure, the members of which have to save their values during the execution of the program, but when any method of any class that uses this structure is accessed, the members can be modified. I understand they need to be static. These members should be in a single instance. Members of this structure will be used by many classes and by the Expert Advisor itself.

It used to be so:

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;       // Уровень заморозки ордеров в пунктах
};

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

But, in this case, it is inconvenient to refer to such variables, because the " name " is long. I.e:

Symbol_Properties::gd_Pt = 0;

How to implement this?

 
hoz:

Let me repeat what I need... There is a structure whose members must save their values during program execution, but when any method of any class that uses this structure is accessed, the members can be modified. I understand they need to be static. These members should be in a single instance. Members of this structure will be used by many classes and by the Expert Advisor itself.

what is the problem with referring to a single created variable?

 

And refer to them by STRUCTURE NAME.MEMBER NAME?

But is there a guarantee that this variable will not lose its current value at runtime?

 
hoz:

And refer to them by STRUCTURE NAME.MEMBER NAME ?

variable name.member name.


But is there a guarantee that this variable won't lose its current value during program execution?

e.g., HQ?

do you have multi-threaded execution in EA?

what do you mean it will lose? what you write there, why should it be lost?

 
в чем проблема обращаться к единственной созданной переменной?

Everyone has the right to live in a world of their own illusions,

but to destroy the world of the userhoz isunusually difficult

because you can't get inside his head and figure out what he's imagining.

"won't lose its current value" - You think variables are created to lose their values?

Which in reality:

Variables havea scope, a storage duration and a linkage.

Learn what it is: Which variable has what scope, when it is visible, when it is created, when it is destroyed and where and when it can be passed to.

Briefly about the storage lifetime:
static (variable declared outside all functions or with static specifier) - time of death = termination of program existence

automatic (a variable declared inside a function) - time of death = leaving the function where it is created

dynamic (variable created using the keyword new) - time of death = use of the keyword delete

 
sergeev:

do you have multithreading in the EA?

what do you mean it will lose? what you put there will be there, why should it be lost?


Opens the documentation here and sees that:

//+------------------------------------------------------------------+
//| Класс "Анализатор текстов"                                       |
//+------------------------------------------------------------------+
class СParser
  {
public:
   static int        s_words;
   static int        s_symbols;
   //--- конструктор и деструктор
                     Parser(void);
                    ~Parser(void){};
  };
...
//--- инициализация статических членов класса Parser на глобальном уровне
int CParser::s_words=0;
int CParser::s_symbols=0;

If class or structure variables don't lose their values during program execution, why are they declared as static here ?

They are in the public section, you can say that as in structure...

 
hoz:

Opens the documentation here and sees that:

If class or structure variables don't lose their values at runtime, why are they declared as static here ?

They are in the public section, you can say that as in structure...


Then... Read the second sentence in the link given to you.

A static in a class has a slightly different meaning than a static in a function.

 
hoz:

If class or structure variables don't lose their values at runtime, why are they declared static here?

They're in the public section, you could say as in the structure...


You are either obtuse or obtuse, sorry for being blunt, but it is written in black and white:

For example, we have aCParserclass intended for parsing texts, and weneed to countthe total number of words and characters processed.It's enough to declare the requiredclass members static and initialize them globally.Then all instances of the classwill use common word and character counters when working.

Where ever there is a reference to:

Class or structure variables lose their values at runtime

It says that preserving OOP principles and not creating global variables, we can count the number of some words with the help of a static class member,

it(static class member) is created as a single instance and doesn't depend on how many objects of a given class are created.

Reason: