Questions on OOP in MQL5 - page 85

 
This is not the first time I've caught myself thinking that in MQL it's better to use OOP only when absolutely necessary and only in a certain part of the code.
 
Pavel Verveyko:
This is not the first time I've caught myself thinking that in MQL it's better to use OOP only in case of urgent need and only in a certain code section.
Who on earth gave you such a stupid idea? If you don't like cats then you simply can't cook them))))
 

I read somewhere and an example appeared on the forum with such a construction

delete &this;

can someone do a simple example with this ?

I'm interested in what this delete &this deletes;

 
Igor Makanu:

I read somewhere and an example appeared on the forum with such a construction

can someone do a simple example with this ?

I am interested in what this delete &this deletes;

It deletes itself)))

 
Vladimir Simakov:

It removes itself)))

that makes sense

but I suspect that the whole point is to use it with static methods

need to test it, really don't know how, that's why I asked


UPD: googled this topic yesterday, lots of mentions of private destructor, also need to think what it can do

 
Igor Makanu:

1) but I suspect the whole point of "delete &this;" - use with static methods
2) yesterday googled this topic, many references to private destructor, also need to think what it can provide

1) From static methods, access to this is forbidden.
Where's "delete &this;" - https://stackoverflow.com/questions/447379/what-is-the-use-of-delete-this

2) The private destructor disallows creating an object on the stack, but an object can still be created through the new operator, this time in the heap:

class A{
   ~A(){printf(__FUNCSIG__);}
public:
    void Delete(){
        delete &this;
    }
};

void OnStart() {
   A* a_ptr = new A();
   a_ptr.Delete();
}

Here is another use of delete &this.

 
Sergey Dzyublik:

1) From static methods, access to this is forbidden.
Where do "delete &this;" - https://stackoverflow.com/questions/447379/what-is-the-use-of-delete-this

2) A private destructor forbids creating an object on the stack, but an object can still be created through the new operator, this time in the heap:

Here is another use of delete &this.

You can always do something with something. But what is the point?

You should invent a name, declare it as a design pattern... and that's it. It's so cool to write a bunch of code instead of just "delete something".

 
A private destructor protects your developments from dilettantes. After all, you can use OOP with them only in a mature way - with new and delete. By the way, have you thought of a name for the "pattern" yet?
 
Dmitry Fedoseev:
You really have a very appropriate nickname ))
 
Igor Makanu:

I read somewhere and an example appeared on the forum with such a construction

can someone do a simple example with this ?

i'm interested in what this delete &this deletes;

this is a pointer to the current object.

Usually the delete this construct is used where an object is created by new, but the responsibility for deleting is on the object itself. In this case, when the object decides it is no longer needed, it calls the deinitialisation function, in which it deletes itself like this.

In my opinion, this is an extremely dangerous practice, acceptable only in case of smartpoints which will count references to the object themselves and then, when number of references becomes zero, they can delete themselves. But even in this case, it seems to me, there is room for hard-to-find memory leakage errors.

In my opinion, the responsibility for deletion should lie with the same object that created it. It can use the object factory pattern when creating it, but deletion should still be the responsibility of the object that created the new object.