Questions on OOP in MQL5 - page 74

 
Vladimir Simakov:

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

My inquisitive mind and naughty hands kept me busy for two days and two nights

i tried to remove dynamically created object out of scope - in the function, groping method, but finally i got the solution:

class CTest
{
public:
   ~CTest() { Print(__FUNCTION__); }
};
//+------------------------------------------------------------------+
void OnStart()
{
   CTest* _test = new CTest;
   deleteCTest(_test);
}
//+------------------------------------------------------------------+
void deleteCTest(CTest* &t)
{
   delete t;
}

that deletes it, but I can't figure it out like this.... and i haven't found any similar function signatures for passing the pointers


Here's another question:

how can I return a pointer to an object by reference as a result of a function?

I want to do something like this:

CTest ret_CTest()
{
   CTest *result = new CTest;
   return(result);
}


it is possible to returnCTest * without problems, but as I wrote ret_CTest() - it gives an error

 
Igor Makanu:

Two days and two nights my inquisitive mind and naughty hands kept me busy

tried to remove dynamically created object out of scope - in the function, gut feeling method, but finally got to the solution:

that deletes it, but I can't figure it out like this.... and i couldn't find any similar function signatures for passing pointers in help or sat

What do you mean it's out of scope? You passed it into a function, so it's visible there)

s.s.void deleteCTest(CTest* &t) without that & should work too, right?

To really remove it outside the scope where it was created, and possibly automatically, you need ... shh... before anyone hears... Pattern Factory... read faster and delete...

 

Igor Makanu:


but how to return a pointer to an object by reference as a result of a function?

you can returnCTest * without problems, but as I wrote ret_CTest() - it gives an error
If you want kosher
CTest& Foo();
, not yet, but if so:
CTest Foo();

If a class has a copy constructor, then it should have a copy constructor.

CTest(CTest &other)...
 
Aleksey Mavrin:

s.w.void deleteCTest(CTest* &t) without this & should work too, or no?

check it out, you'll learn something new ;)

I've written many times, that the MQL pointers are a very instructive thing ))))


Vladimir Simakov:
If you want kosher, not yet, but if so:

If you want to use a pointer, the class must have a copy constructor.

Again, I need to think

I have a suspicion that GetPointer() may do some tricks... but it's not a fact.... These "jumps through space and time" - via pointer and implicit dereferencing in MQL - are out of my mind ! (((

 
Igor Makanu:

check it out, you'll learn a lot ;)

I've written many times that pointers in MQL are a very instructive thing ))))

It deletes pointers but it cannot assign a new value to _test. By the way, MQL has nothing to do with it)))

 
Igor Makanu:

check it out, you'll learn a lot ;)

I've told you over and over again that pointers in MQL are very instructive ))))


Again, I need to think

I have a suspicion that GetPointer() may do some tricks... but it's not a fact.... These "jumps through space and time" - via pointer and implicit dereferencing in MQL - are out of my mind! (((

It's OK here)))) Just turn off the sharpe, there's not even a hint of it here))))

 
Igor Makanu:

check it out, you'll learn a lot ;)

I've told you over and over again that pointers in MQL are very instructive ))))

I checked it. It deletes well without &)

I tried it in MT4 and MT5 and compare )

class CTest
{
public:
   CTest() { Print(__FUNCTION__); }
   ~CTest() { Print(__FUNCTION__); }
};
//+------------------------------------------------------------------+
void OnStart()
{
   CTest* _test = new CTest();
   deleteCTest(_test);
}
//+------------------------------------------------------------------+
void deleteCTest(CTest* t)
{
   delete t;
}
 
Aleksey Mavrin:

Well checked. It deletes perfectly without &)

I tried it in MT4 and MT5 and compare )

Not a complete example, i ran into it today.

It will not work without & .

class CTest
{
public:
   ~CTest() { Print(__FUNCTION__); }
};
//+------------------------------------------------------------------+
void OnStart()
{
   CTest* _test = new CTest;
   deleteCTest(_test);
   delete _test;
}
//+------------------------------------------------------------------+
void deleteCTest(CTest* & t)
{
   delete t;
   t = new CTest;
}
//+------------------------------------------------------------------+

Vladimir Simakov:

I am fine here))) I'm not looking for sharps but I'm sure it will work without &)).


I'm not looking for Sharp, I'm still dealing with MQL, I may ask what I do not understand.

 
Igor Makanu:

not a complete example, I came across it today.

it won't work that way without &

Sure it will. You're passing a pointer by value, and therefore a copy of the pointer will be created. You'll nail the object through it, and create a new object for the pointer that's created in the function. This is c++, welcome))))
 
Igor Makanu:

not a complete example, I came across it today.

it won't work without &



In my opinion, it has nothing to do with references, but only with the fact that the function argument must be passed by pointer if you want it to save the value after the function is running. The same will happen with a file or indicator handle, for example, which are not references.

This has always been the standard in C++, although it's been a long time since I vaguely remember.