PLO - page 8

 

How do I return a reference in function parameters?

This kind of code causes problems:

CMy* obj; // global
bool func(CMy* obj)
{
...
  obj = new CMy();
...
}

Invalid pointer access when trying to work with obj.

Tried adding an &.

bool func(CMy*& obj)

It works. I didn't find the reference return in function parameters in the documentation.

By analogy, I found this topic in C++: http://stackoverflow.com/questions/5286453/returning-a-pointer-as-a-function-parameter

But ** does not work here. *& instead of it?

5.00.630 x64

returning a pointer as a function parameter
returning a pointer as a function parameter
  • stackoverflow.com
Im trying to return the data pointer from the function parameter: bool dosomething(char *data){ int datasize = 100; data = (char *)malloc(datasize); // here data address = 10968998
 
flops:

Return reference in function parameters was not found in the documentation.

You mean this: MQL5 Reference / Language Basics / Data Types / References. Modifier & and keyword this?
 
flops:

How do I return a reference in function parameters?

This kind of code causes problems:

Invalid pointer access when trying to work with obj.

Tried adding an &.

It works. I didn't find the reference return in function parameters in the documentation.

By analogy, I found this topic in C++: http://stackoverflow.com/questions/5286453/returning-a-pointer-as-a-function-parameter

But your ** doesn't work. *& instead of it?

5.00.630 x64

IMHO (it was long ago).

You cannot pass a pointer through arguments but create a reference. You cannot bind a new instance of a class to a reference. That leaves the following way:

CMy* func()
{
...
  CMy* obj;
  obj = new CMy();
  return(obj);
...
}
 
Vigor:

I am a proponent of OOP, because I have understood the convenience of programming from the bottom up. From object interaction interfaces to class implementation.

Isn't an interface external to a class?
 
220Volt:

IMHO (it was a long time ago).

You cannot pass a pointer through arguments, you can create a reference. A new instance of the class cannot be attached to the reference. That leaves us with this option:

I have defined the question incorrectly.

Passing a pointer by reference. I'm wondering if one may use the *& variant and won't they cover it in further versions? In C++, such a construct seems to be legal.

 
Yedelkin:
You're referring to it: MQL5 Reference Guide / Language Basics / Data Types / References. Modifier & and keyword this?

I wish there was the right information...


In the article

https://www.mql5.com/ru/docs/basis/function/ParameterPass

you can also add information about working with objects.

Документация по MQL5: Основы языка / Функции / Передача параметров
Документация по MQL5: Основы языка / Функции / Передача параметров
  • www.mql5.com
Основы языка / Функции / Передача параметров - Документация по MQL5
 
Andrei01:
Isn't an interface external to the class?

What difference does it make for objects? An interface in this interpretation is a data type, because its description clearly enough defines the properties of objects. And, in fact, it defines the external behaviour of objects.

In the materials I cited, the opponents of OOP mentioned that it is impossible to create the class structure immediately without implementing the algorithm, which is why the OOP environment so often practises refactoring and why classes slow down development with their eternal redesigning. And this is really true. When you implement something new and don't understand what it can lead to, you have to redo classes and more than once.

Creating a good class architecture is a stumbling block for beginners. In essence, the point of OOP programming is to write your own"language", a language that is easy and fast to create in the future. Interface is the essence of a language which is designed for the task. Interface, as a matter of fact, must not be understood as a construction "of type" of the class, as a contract for the class, which must be executed in code (in MQL, it doesn't exist and the role of interface can be feebly performed by virtual methods).

For example, you can create a "language" for creation of new EAs that allows you to assemble the general scheme of EAs from objects in a few lines. Signals and conditions can be used ready-made, or you can develop your own classes of signals and create your own library by analogy. This possibility is provided by MQL5 Wizard.

Often, the application's scheme has already been developed and "OOP programming" essentially boils down to the use of existing classes with limited degrees of freedom for writing your own functionality - this is convenient. This is why frameworks are so popular. They eliminate unnecessary problems and headaches when developing application architecture. In fact, writing something of your own is creating an heir class of one of the framework classes, and writing implementation of certain backend methods in it, which are already built into the overall application logic (OnInit, OnTick, for example).

 
Vigor:

...

Creating a competent class architecture is a stumbling block for beginners. In essence, the point of OOP programming is to write your own"language", a language that is easy and fast to create in the future. Interface is the essence of a language which is designed for the task. Interface should not be understood as a construct of "type" of the class, as a contract for the class, which must be executed in code (in MQL, it does not exist, and the role of interface can be validated by virtual methods).

...

The point of OOP is rather to write your own architecture. As soon as we have a well-defined, clear and intuitive architecture, everything falls into place and neither project complexity nor its volume are the constraining factors. However, in order to achieve this very clear architecture you have to rewrite classes one by one getting closer to this initially vague ideal.
 
C-4:
Rather, the point of OOP is to write your own architecture. As soon as we have a clear, concise, intuitive architecture, everything falls into place and neither the complexity of the project nor its scope are any more constraining factors. However in order to achieve this very clear architecture you have to rewrite classes one by one getting closer to this initially vague ideal.
+1