Questions on OOP in MQL5 - page 6

 
Dmitry Fedoseev:

So there you go.

Like this:

Not even just can, but better, because you can call it like this:

If you pass a pointer by reference, you need another variable for the pointer.

***

And if you have to pass a pointer to a function to create an object in the function, that's how it works:

That's basically all you wanted to know about OOP, but were afraid to ask)))

Nuh-uh

 
void z(CObj* & o){
   o = new CObj();
}

And just think of it this way, it all works.

So, if you have any questions about OOP, feel free to))))

 
A classic null pointer dereferencing turned out.
 
Vladimir Simakov:

So if you have any questions about OOP, feel free to))))

Please explain the meaning of creating a dynamic object using the new operator.

When an object is created automatically, a class object is created in the stack and is faster than a dynamic object in terms ofexecution time.
When an object is created dynamically, the class object is created in memory (in the heap) and the OS memory manager is activated, the process is slower.

Here are the questions:
If automatic creation is faster, then why is it better to use dynamic objects?
Explicitly control memory allocation?
Eliminate a possible stack overflow?
And not to lose an object unexpectedly?
Because if the stack overflows, the object will be automatically deleted?

 
Roman:


Aren't you tired of keeping up with the topic?

 
Artyom Trishkin:

Aren't you tired of keeping up with the topic?

Adjusting the questions to be as accurate as possible.
Is there anything wrong with that? There's an edit button, so you can use it to make a point.

 
Roman:

Please explain the meaning of creating a dynamic object with the new operator.

When an object is created automatically, the class object is created in the stack, which is faster than a dynamic object in terms of execution time.
When an object is created dynamically, the class object is created in memory (in the heap) and the OS memory manager is activated, the process is slower.

Here are the questions:
If automatic creation is faster, then why is it better to use dynamic objects?
Explicitly control memory allocation?
Eliminate a possible stack overflow?
And not to lose an object unexpectedly?
Because if the stack overflows, the object will be automatically deleted?

Why don't you read about static memory, stack and heap?
 
Roman:

Please explain the meaning of creating a dynamic object with the new operator.

One clear example of use is when the type of object is not known beforehand.

Here's a good example of this in the help: https://www.mql5.com/ru/docs/basis/operators/newoperator

Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
  • www.mql5.com
//| Создание фигуры                                                  |
 
Vladimir Simakov:

Nuh-uh

Yeah! Except everything compiles and works for some reason.

What kind of miracles you have there... No one knows but, by the way, not surprising after yesterday's code samples supposedly taken from somewhere else in this thread but actually missing somewhere else.

***

Oh, and in case you haven't forgotten what we were originally talking about - about passing a pointer to a function or method (and about what you claimed that some memory leakage is allegedly going on there), which is exactly where & isn't needed.


 
Roman:

Please explain the purpose of creating a dynamic object through the new operator.

When an object is created automatically, the class object is created in the stack and is faster than a dynamic object in terms of execution time.
When creating an object dynamically, a class object is created in memory (in the heap), thus involving OS memory manager, the process is slower.

Here are the questions:
If automatic creation is faster, then why is it better to use dynamic objects?
Explicitly control memory allocation?
Eliminate a possible stack overflow?
And not to lose an object unexpectedly?
Because if the stack overflows, the object will be automatically deleted?

If the number of objects is known beforehand and it is constant during the running of the program, then new is not needed. In all other cases - new.

Reason: