Questions on OOP in MQL5 - page 12

 
Vasiliy Pushkaryov:

A word of advice on another issue. If you create a child class CMyButton from CButton, you can create a button and then change its properties outside the class. Below this is done in OnInit().

But if I want to make additional fields inside the child class and use the built-in properties of the CButton class in new functions, how can I do this correctly?

In the CButton class, the m_button class member is declared in the private section.

I can't get into the private section of the parent class.

Maybe we could keep it simple and straightforward - look at how the name of the graphical object is formed in the CButton class and work directly with those graphical objects. The more so with a button, it should be easy, it only has one graphical object.

 
TheXpert:

Yep, already two unconnected people are photoshopping a cross of your code )

Your code cannot work properly - it's obvious from the code itself).

Two people? Out of your imagination, huh? You should at least read more carefully, or at least look at the pictures more carefully. The first message is from a debugger. It compiles and works without the debugger.

 
Dmitry Fedoseev:

Two people? From your fantasy, huh? You should read more carefully, or at least look at the pictures more carefully. The first message is from a debugger. Without debugger it compiles and works.

I got it crashing without debugger.)

it crashes in debugger too ))

maybe you just don't know where to look at the script's output log? )))

 
TheXpert:

I'm crashing without debugger)

I'm cresting in debugger too.)

maybe you just don't know where to view the script's output log? )))

Nah, I don't know, tell me where it is.

 
Vasiliy Pushkaryov:

A word of advice on another issue. If you create a child class CMyButton from CButton, you can create a button and then change its properties outside the class. Below this is done in OnInit().

But if I want to make additional fields inside the child class, and use the built-in properties of the CButton class in new functions, how can this be implemented correctly?

In CButton class the m_button class member is declared in private section.

I think you're solving your problem from the "tail" side again, I would suggest to solve it from the other side:

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

void CMyButton::setButton(void)
{
  mybtn = new CButton;
  mybtn.Create(....................);
  // как в этой функции создать кнопку? Я не могу вызвать метод Create()


Dmitry Fedoseev:

You cannot get into the private section of the parent class.

you can get in at least with #define and you can experiment with dynamic_cast. Last year I studied dynamic_cast and it does amazing things, but it is a time consuming process


TheXpert:

i got it without debugger )

checked - I also got: 2019.07.05 17:10:27.307 1 (EURUSD,H1) invalid pointer access in '1.mq5' (43,6)

 

But you intrigued me, so I thought maybe I really am the idiot you think I am. I dug up some code I wrote five years ago, here's a snippet:

So, you can keep... spew your... all you want... ...and dream that I'm an idiot, but so far it's the other way around. You're jumping around like mad devils and it's no use.

***

And with that code of mine, despite the fact that it compiles and works, it quickly becomes clear that there's something wrong with it. The object is not deleted via delete.

But you like to believe that I don't even know what: CObj obj; go on believing...

***

But they are ready to freeze their ears to their heels "to spite their grandfather". Even their rubbish collector appeared in MQL.

 
Dmitry Fedoseev:

And you intrigued me, so I thought maybe I really am the idiot you think I am. I dug up some code I wrote five years ago, here's a snippet:

So, you can keep... spew your... all you want... ...and pipe down that I'm an idiot, but so far it's the other way around.

Here everything is correct, the reference is to a pointer, and the one that's causing all the fuss is a reference to an object that doesn't exist. Look through the code carefully.
 
Vladimir Simakov:
Everything is correct here, the pointer is referred to, while the one that makes the whole fight about is a reference to an object that doesn't exist. Look through the code carefully.

Do you really think that I can't notice that there is a * in one place and an * & in another?

And do you also believe that I don't know whatCObj obj; is?

 
Vladimir Simakov:
...a reference to an object that doesn't exist...

But it compiles and works.

 
Dmitry Fedoseev:

Do you really think that I can't notice that there is a * in one place and an * & in another?

And do you also believe that I don't know whatCObj obj; is?

You did.
void z(CObj & o){
   o = new CObj();
}
Reason: