- Integer Types
- Real Types (double, float)
- Complex number (complex)
- String Type
- Structures, Classes and Interfaces
- Dynamic Array Object
- Matrices and vectors
- Typecasting
- Void Type and NULL Constant
- User-defined Types
- Object Pointers
- References: Modifier & and Keyword this
User-defined types
The typedef keyword in C++ allows creating user-defined data types. To do this, simply specify a new data type name for an already existing data type. The new data type is not created. A new name for the existing type is defined instead. User-defined types make applications more flexible: sometimes, it is enough to change typedef instructions using substitution macros (#define). User-defined types also improve code readability since it is possible to apply custom names to standard data types using typedef. The general format of the entry for creating a user-defined type:
typedef type new_name; |
Here, type means any acceptable data type, while new_name is a new name of the type. A new name is set only as an addition (not as a replacement) to an existing type name. MQL5 allows creating pointers to functions using typedef.
Pointer to the function
A pointer to a function is generally defined in the following format
typedef function_result_type (*Function_name_type)(list_of_input_parameters_types); |
where after typedef, the function signature (number and type of input parameters, as well as a type of a result returned by the function) is set. Below is a simple example of creating and applying a pointer to a function:
//--- declare a pointer to a function that accepts two int parameters
|
In this example, the func_ptr variable may receive the sub and add functions since they have two inputs each of int type as defined in the TFunc pointer to the function. On the contrary, the neg function cannot be assigned to the func_ptr pointer since its signature is different.
Arranging event models in the user interface
Pointers to functions allow you to easily create processing of events when creating a user interface. Let's use an example from the CButton section to show how to create buttons and add the functions for handling pressing to them. First, define a pointer to the TAction function to be called by pressing the button and create three functions according to the TAction description.
//--- create a custom function type
|
Then, create the MyButton class from CButton, where we should add the TAction pointer to the function.
//+------------------------------------------------------------------+
|
Create the CControlsDialog derivative class from CAppDialog, add the m_buttons array to it for storing the buttons of the MyButton type, as well as the AddButton(MyButton &button) and CreateButtons() methods.
//+------------------------------------------------------------------+
|
Now, we can develop the program using the CControlsDialog control panel having 3 buttons: Open, Save and Close. When clicking a button, the appropriate function in the form of the TAction pointer is called.
//--- declare the object on the global level to automatically create it when launching the program
|
The launched application's appearance and button clicking results are provided on the screenshot.
The full source code of the program
//+------------------------------------------------------------------+
|
See also