Questions on OOP in MQL5 - page 14

 
TheXpert:

is also in the custody of the moderators

Whoever said anything about custody... but a character with two eccentricities...

 
TheXpert:

also under the tutelage of the moderators

I thought I deleted my own post.
 
Vladimir Simakov:
I seem to have deleted my own post.

This was the case when an object is created in a function via new. Originally, we talked about passing a pointer to an existing object. And here, it can be passed like this:

void f(CObj * obj){}

 
Dmitry Fedoseev:

And if a pointer needs to be passed to a function to create an object in the function, that's how it works:

class CObj{
   public:
   int f(){
      return(33);
   }
};

CObj * obj;

void OnStart(){
  z(obj);
  delete(obj);
}

void z(CObj & o){
   o = new CObj();
}
That's all you wanted to know about OOP, but were afraid to ask)))

This code kicks out the terminal for me. At best, it causes a critical error in debugging (for animation click on the picture):


Strange that such a construct gets skipped by the compiler.

 
Guys, stop fighting amongst yourselves, especially on a level playing field.
Dmitry gave the first example with an error, and what's wrong with that?
Dim, yes it compiles without errors, but when you run the script, an error occurs.

Vladimir corrected it and added a pointer, the code worked correctly.
What else do we need? Now we have to find out who is the coolest? ))))
Guys, be respectful of each other, and do not resort to personalities and insults with arrogance.

Initial example from Dimitri
class CObj{
   public:
   int f(){
      return(33);
   }
};

CObj * obj;


void OnStart(){
  z(obj);
  delete(obj);
}

void z(CObj & o){     //тут пропущен указатель
   o = new CObj();
}
Correction by Vladimir.
void z(CObj* &o){    //добавлен указатель
   o = new CObj();
}
My correction with a print of the method result, to verify
This example is now working, what more do you need?
Stop trying to figure out who is cooler or not, everyone has typos and errors.
class CObj
{
   public:
   int f(){return(33);}
};

void z(CObj* &o)
{
   o = new CObj();
   Print(o.f());
}

CObj * obj;

//+------------------------------------------------------------------+
void OnStart()
{      
  z(obj);
  delete(obj);
}
In the course of these arguments, I think we've identified a compiler error.
Why does the compiler skip compiling Dmitri's initial example without errors?
 
Vasiliy Sokolov:

This code kicks out the terminal for me. At best, it causes a critical debugging error:

It's strange that such a construct gets passed by the compiler.

Everything is valid for the compiler here.

A pointer to an object in the program data segment is created and is 0x0 initiated during compilation.

Necessary commands to the processor are created in the code segment.

At runtime, commands are raised on the stack at the address of the z(...) function.

And there move rdx [0x0]

And accessing a null memory address is forbidden. The dereferencing of a null pointer is called

 
Vladimir Simakov:
Everything is valid for the compiler here.

For which compiler?

 
Vasiliy Sokolov:

This code kicks out the terminal for me. At best, it causes a critical error in debugging (for animation click on the picture):


Strange that such a construct gets skipped by the compiler.

Mine compiles, works correctly, but shows a memory leak (delete doesn't work) message on completion.

 
Igor Makanu:

You'll teach him the bad stuff now, it's the good stuff that needs to be dealt with )))

#define private protected

You didn't sit with the fxsaber codes for nothing. It sounds like a masterpiece to me.

 

Apologies for the misinformation, I wrote earlier that you can't call the Create() method in a child class.

Now double-checked, you can. But access to private member m_button was unavailable. I.e. it was impossible to check, for example, button's state: pressed/released.

Now with Igor's hint it's possible.

#define private protected
#include <Controls\Button.mqh>

class CMyButton : public CButton
{ 
  public: 
              CMyButton(void){}; 
             ~CMyButton(void){}; 
             
        bool    isPrevState;        // состояние кнопки на предыд.тике, true - была нажата     
        void    setButton();        // создаем кнопку
}; 

void CMyButton::setButton(void)
{
  // метод Create() вызывается
   Create(0, "setBtn", 0, 50, 300, 150, 325);
   Text("setBtn");
   
   m_button.State();     // работает когда включаем #define private protected
}