MQL5 The compiler does not distinguish between a class and a pointer to it - page 7

 
Alexey Navoykov:
So you're proposing to ban implicit naming of pointers altogether? I don't think many people here would be happy about that.

Let everyone have a choice.

#property strict

I certainly don't want that kind of trick.

 
SemenTalonov:

Well, that goes without saying. There's a memory leak.

A memory leak, you say? How about this? )))

#property strict

class A
 {
  A*  item;
  int num;
public:
  A(int p=10){num=p;item=NULL;} ~A(){if(CheckPointer(item)==POINTER_DYNAMIC) delete item;}
  A* operator=(A* par){ item = par; return&this; }
  A* operator~(){ return item; }
  int operator-(){ return num; }
 };

void OnStart()
 {
  A a = new A(20);
  printf("В автообъекте %i (%i) сохранен динамический объект %i (%i), который он в конце своего цикла сам уничтожает...",&a,-a,~a,-~a);
 }


 
Ilya Malev:

A memory leak, you say? How about this? )))


delete item;

So it assumes that the subject is prepared to be treated that way. How many foresee this possibility?

 
SemenTalonov:

So this assumes that the object is prepared to be treated in this way. Do many people provide for such a possibility?

I think that if someone writes the word new, they should know exactly where the corresponding word delete is located elsewhere in their code.

 
This construction, by the way, if a little bit refined, is called a "rubbish collector")))
 
SemenTalonov:

Let everyone have a choice.

I certainly don't need such tricks.

I already use it in MQL4. How will it work then, if language syntax is the same?

Well, as a protection against such actions one can create a private method in a class:

class A
{
 private: void operator=(const A*);
}

Although, frankly, I don't see a problem here. If you assign something to a class object, you a priori expect that the copy operator will be called. You want it, you get it. What difference does it make if there was a pointer or an object? The class type is the same. You can't assign any left-handed class. That's why it all looks more like an idle bore.

The opposite case, with the pointer on the left, is another matter. There you could have expected to assign something to the pointer, but it turned out to be copying an object.

 
Alexey Navoykov:

The opposite case, with the pointer on the left, is different. There you might expect to assign something to the pointer, but it turns out you're copying an object.

And you can't forbid this, otherwise you won't be able to copy the object when you really need to.

As a result, you have to leave everything as it is. Just be aware of what you are doing.

 
fxsaber:

And you can't forbid it, otherwise you won't be able to copy the object when you really need to.

In the end, you have to leave it as it is. Just be aware of what you're doing.

О! That's the main thing.

Are there many people here who know how to navigate the modes of memory management? Understanding the difference between AUTOMATIC and DYNAMIC.

And have heard something about STATIC and BASED (although this is not necessary).

 
fxsaber:

And you can't forbid it, otherwise you won't be able to copy the object when you really need to.

In the end, you have to leave it as it is. Just be aware of what you are doing.

Why can't it work? There are * and & operators for converting to what you need.
 
Alexey Navoykov:
Why can't it work? There are * and & operators to reduce it to what is required.

Where is there a *?

Reason: