Questions on OOP in MQL5 - page 73

 
Igor Makanu:

Lowering yourself to the appropriate level of communication: is my question on the topic of the thread? Why did you come here with your pictures from the Internet? Do you know the answer? - No, sit and shut up ;)

First, do not poke me, and second, do not react so violently to the truth.
I have to be honest with you, I get the impression that you are not very far from your interlocutor today:

Any questions from beginners in MQL4 and MQL5, help and discussion of algorithms and codes

Igor Makanu, 2020.06.07 13:46

Hello all. Please help me insert a String variable into my code. Thanks to

could you answer your own question?
or did you cut out a piece of code somewhere and can't use it? and you have to guess what's going on at least))
......
but i guess that's not the answer with this approach

 

a notoriously kind moderator has promised to ban you for provoking a verbal response :-)

waiting...

 
Some know more, some know less. The forum is for communication and Q&A.
There is no shame in asking a question to enrich knowledge. It is embarrassing to behave in a condescending manner towards the questioners - this is the level of a student who has moved on to the next course and is broadcasting "sacred knowledge through the lip". A total zanyness in general...
 
Maxim Kuznetsov:

a notoriously kind moderator has promised to ban you for provoking a verbal response :-)

wait for it...

It is difficult to make me angry, and I have used all curse words in the army, there is nothing without them )))

 
Maxim Kuznetsov:

a notoriously kind moderator has promised to ban you for provoking a verbal response :-)

waiting...

No, Maxim, not so. Please re-read it with an open mind.
 
#include <JSON\json.mqh>

JSONObject * getJSONObject(const string json)
{
   JSONValue *jv =((JSONParser *)(new JSONParser())).parse(json);   //1
   if (jv != NULL && jv.isObject()) return((JSONObject *)jv);       //2
   Print(__FUNCSIG__ + "parser error, json = ",json);
   delete jv;                                                       //3
   return(NULL);
}
void OnStart()
{
   JSONObject *jobj = getJSONObject("{\"ObjType\":2,\"m_period\":1}");
   if(jobj!=NULL) Print("m_period = ", jobj.getInt("m_period"));    
   delete jobj;                                                     //3
}


1. no delete for object ofJSONParser type created via new
meaningless casting to
JSONParser*
What's wrong with a localJSONParser variable? Or did you want to use a temporary object but don't know its signature?

2. Pointless casting inJSONObject*
3
. The jv and jobj variables will definitely not be NULL? Otherwise removing a null pointer in MQL will print "rubbish" to the log.

 
Sergey Dzyublik:


1. no delete for object ofJSONParser type created via new
meaningless casting to
JSONParser*
What's wrong with a localJSONParser variable? Or did you want to use a temporary object but don't know its signature?

2. Pointless casting inJSONObject*
3
. The jv and jobj variables will definitely not be NULL? Otherwise removing a null pointer in MQL will print "rubbish" to the log.

Thank you, at least that's something.

this method was used by the author of the library, i don't see the point in writing calls for parsing in another way


i don't understand the situation with MQL when working with pointers - you can't dereference them when the pointer becomes an entity, and when it's a pointer to an object - i don't know what to do ((

SZY: I studied C++ 20 years ago, it's not widely used for production engineers later, first they all sat on Delphi, then crawled over to Sharp, for controllers too, I haven't seen C++, everything works onspecialised software - it's probably the stamp of universities for engineering students - they always start training with Pascal, even at Volgograd Polytechnic they read turbo-pascal....Who knows where they get the compilers ))))


ZS: remove the chorus, it's not nice, it's usually the other way round.

 
Igor Makanu:

Thanks, that's something.

the meaningless is not applicable, this method was used by the author of the library, i don't see the point in writing other way of parsing calls


with MQL the situation is not clear when dealing with pointers - you can't dereference them when the pointer becomes an entity, and when it's a pointer to an object - i don't know what to do (((

SZY: I studied C++ 20 years ago, it's not widely used for production engineers later, first they all sat on Delphi, then crawled over to Sharp, for controllers too, I haven't seen C++, everything works onspecialised software - it's probably the stamp of universities for engineering students - they always start training with Pascal, even at Volgograd Polytechnic they read turbo-pascal....Who knows where they get the compilers ))))


SZY: remove the chorus, it's not nice, it's usually the other way round.

There are no pointers here) There's a handle for the class, yes working with it is similar to working with a pointer, but that's where the common thing ends.

Now for the highlighted one:

  • You create an object either on the stack or in the heap.
    CTest test;             //test - object
    CTest* test=new CTest;  //test - handle of object
    In the first case, the compiler adds an implicit destructor call, which doesn't clean up any memory; it just returns the stack pointer to the position before the function is called. In the second case, when leaving the scope, the memory occupied by the object remains marked as occupied by the memory manager and since no pointer points to it anymore, it cannot be freed (this is called a memory leak), so we have to explicitly destroy the object through delete.
  • As for local run-time. Here handle, if there is no overloaded function especially for it, is implicitly dereferenced, example:
    class CTest{
    };
    
    void OnStart()
      {
       CTest test;
       CTest* _test=new CTest;
       Test(test);
       Test(&test);
       Test(_test);
       _Test(_test);
       delete _test;
      }
      
    void Test(CTest &test){Print("Object");}
    void Test(CTest* test){Print("Handle");}
    void _Test(CTest &test) {Print("Ok");}
    PS. Yes, and forget about Sharp, nothing in common at all))))
 

Vladimir Simakov:

PS. Yes, and forget about Sharp, nothing in common)))

You won't get it, you get used to the good stuff all the time, it's logical, if you mess up - VS will tell you right away, and when the code is executed - you leave the function body - just forget about it, you just assign NULL to the created code and it removes the mess by itself ))

Thanks, I will think about it

 
Igor Makanu:

It won't work, you get used to a good thing very quickly, it's all logical, if you mess up somewhere - VS will tell you right away, and in code execution you leave the function body - just forget, at most you'll just assign NULL to it - it will clear it up by itself ))

Thanks, I will think about it

Yeah, only one sad implicit object reference (e.g. method call in invoke leaf of some delegate) lives in the code thicket and the memory is gone. Can't make a mess of it in Sharp, either.