Calling constructor of the super class

 

Hi

Can someone please explain what this part of the code mean as found in the file  panelDialog.mqh  downloadable from the link below?

I mean this part of the constructor.

: m_red(blah...),

What is this convention called so I can read up on it? 

I could not find m_red in the file Dialog.mqh in the standard libarary.


Many thanks

https://www.mql5.com/en/articles/345


class CPanelDialog : public CAppDialog {
private:
int m_red;
int m_green;
// stuff
};
CPanelDialog::CPanelDialog(void) : m_red((BASE_COLOR_MAX-BASE_COLOR_MIN)/2),
                                   m_green((BASE_COLOR_MAX-BASE_COLOR_MIN)/2)
  {
  }
Create Your Own Graphical Panels in MQL5
Create Your Own Graphical Panels in MQL5
  • www.mql5.com
There is a new set of classes available in the Standard Library. These classes are designed for independent development of control dialogs and display panels in MQL5 programs. The new set of classes enables everyone to create custom interface components using the event-driven model as the underlying model. Everything is based on the embedded...
 

Do not double post.

I have deleted your other topic.

 
samjesse:

Hi

Can someone please explain what this part of the code mean as found in the file  panelDialog.mqh  downloadable from the link below?

I mean this part of the constructor.

: m_red(blah...),

What is this convention called so I can read up on it? 

I could not find m_red in the file Dialog.mqh in the standard libarary.


Many thanks

https://www.mql5.com/en/articles/345


This is not a constructor. This is called initialisation list, where you can set initial values for class members (comes very handy for setting values for const members)

 

Thanks

Another question please since it was deleted "claiming double posting" by the moderator.



I found the following code in panelDialog.mqh downloadable from the link below, and could not find a google link to explain what it means and does and it's usefulness.

Any help or where to read up on it?

Thanks


class CPanelDialog : public CAppDialog { 
 // stuff
EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT(ON_CHANGE,m_edit_blue,OnChangeBlue)
EVENT_MAP_END(CAppDialog)
 

To answer your first question, you can call the super class' constructor from the subclass constructor. Consider the following: 


class Base
{
protected:
   const string m_salutation;
public:
   Base(const string salutation):m_salutation(salutation){}
   string to_string() { return m_salutation; }
};

class SubClass : public Base
{
public:
   SubClass():Base("Hello world!"){}
   void print(){ Print(this.to_string()); }
};


On to your next question... The code you posted is using the event handling MACROS which are located in MQL4\Include\Controls\Defines.mqh

#define INTERNAL_EVENT                           (-1)
//--- beginning of map
#define EVENT_MAP_BEGIN(class_name)              bool class_name::OnEvent(const int id,const long& lparam,const double& dparam,const string& sparam) {
//--- end of map
#define EVENT_MAP_END(parent_class_name)         return(parent_class_name::OnEvent(id,lparam,dparam,sparam)); }
//--- event handling by numeric ID
#define ON_EVENT(event,control,handler)          if(id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()) { handler(); return(true); }
//--- event handling by numeric ID by pointer of control
#define ON_EVENT_PTR(event,control,handler)      if(control!=NULL && id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()) { handler(); return(true); }
//--- event handling without ID analysis
#define ON_NO_ID_EVENT(event,handler)            if(id==(event+CHARTEVENT_CUSTOM)) { return(handler()); }
//--- event handling by row ID
#define ON_NAMED_EVENT(event,control,handler)    if(id==(event+CHARTEVENT_CUSTOM) && sparam==control.Name()) { handler(); return(true); }
//--- handling of indexed event
#define ON_INDEXED_EVENT(event,controls,handler) { int total=ArraySize(controls); for(int i=0;i<total;i++) if(id==(event+CHARTEVENT_CUSTOM) && lparam==controls[i].Id()) return(handler(i)); }
//--- handling of external event
#define ON_EXTERNAL_EVENT(event,handler)         if(id==(event+CHARTEVENT_CUSTOM)) { handler(lparam,dparam,sparam); return(true); }
//--- handling the rest of unhandled events
#define ON_OTHER_EVENTS(handler)                 if(handler(id,lparam,dparam,sparam)) return(true);

This design pattern allows you to circumvent the limitations of the MQL language and pass functions into the event handler psuedo-function. To make this work consider the following hello world gui example. 

#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict

#define INDENT_LEFT     (8)       // left indent (including the border width)
#define INDENT_TOP      (8)       // top indent (including the border width)
#define EDIT_WIDTH      (100)     // size along the X-axis
#define EDIT_HEIGHT     (20)      // size along the Y-axis

#include <stdlib.mqh>
#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
//+------------------------------------------------------------------+
class HelloWorldGui : public CAppDialog
{
protected:
   CButton           m_say_hello_obj;
public:
   virtual bool      Create(  const long chart, const string name, const int subwin,
                              const int x1,const int y1,const int x2,const int y2);
 
   virtual bool      OnEvent(const int id,const long& lparam,const double& dparam,const string& sparam);
protected:
   bool              _create_button();
   void              _say_hello_func();
};
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(HelloWorldGui) //this class' name
   ON_EVENT(ON_CLICK, m_say_hello_obj, _say_hello_func) //1: event, 2: object, 3: event func
EVENT_MAP_END(CAppDialog) // super class' name
//+------------------------------------------------------------------+
bool HelloWorldGui::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
{
   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2)
      || !this._create_button()
   ) 
      return false;
   return true;
}
//+------------------------------------------------------------------+
bool HelloWorldGui::_create_button(void)
{
   int x1 = INDENT_LEFT;
   int y1 = INDENT_TOP;
   int x2 = x1 + EDIT_WIDTH;
   int y2 = y1 + EDIT_HEIGHT * 2;
   if(!m_say_hello_obj.Create(m_chart_id, m_name+"_close_this", m_subwin, x1, y1, x2, y2))
      return false;
   m_say_hello_obj.Text("Say Hello");
   m_say_hello_obj.ColorBackground(clrOrange);
   if(!this.Add(m_say_hello_obj))                                                            
      return(false);
   return true;
}
//+------------------------------------------------------------------+
void HelloWorldGui::_say_hello_func(void)
{
   Alert("Hello world!");
}
//+------------------------------------------------------------------+
HelloWorldGui gui;

int OnInit()
{
   if(!gui.Create(0, "Hello", 0, 100, 100, 225, 190)
      || !gui.Run()
   )
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
   gui.Destroy();
}
void OnTick(){}
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   gui.ChartEvent(id, lparam, dparam, sparam);
}
 
Much greatfull for such a generious response :)
Reason: