Structures and Classes
Structures
A structure is a set of elements of any type (except for the void type). Thus, the structure combines logically related data of different types.
Structure Declaration
The structure data type is determined by the following description:
struct structure_name |
The structure name can's be used as an identifier (name of a variable or function). It should be noted that in MQL5 structure elements follow one another directly, without alignment. In C++ such an order is made to the compiler using the following instruction:
#pragma pack(1) |
If you want to do another alignment in the structure, use auxiliary members, "fillers" to the right size.
Example:
struct trade_settings |
Such a description of aligned structures is necessary only for transferring to imported dll-functions.
Attention: This example illustrates incorrectly designed data. It would be better first to declare the take and stop large data of the double type, and then declare the slippage member of the double type. In this case, the internal representation of data will always be the same regardless of the value specified in #pragma pack().
If a structure contains variables of the string type and/or object of a dynamic array, the compiler assigns an implicit constructor to such a structure. This constructor resets all the structure members of string type and correctly initializes objects of the dynamic array.
Simple Structures
Structures that do not contain strings or objects of dynamic arrays are called simple structures; variables of such structures can be freely copied to each other, even if they are different structures. Variables of simple structures, as well as their array can be passed as parameters to functions imported from DLL.
Access to Structure Members
The structure is a new type of data allowing to declare variables of this type. The structure can be declared only once within a project. The structure members are accessed using the point operation (.).
Example:
struct trade_settings |
Classes differ from structures in the following:
Classes and structures can have an explicit constructor and destructor. If you a constructor is explicitly defined, the initialization of a structure or class variable using the initializing sequence is impossible.
Example:
struct trade_settings |
Constructors and Destructors
A constructor is a special function, which is called automatically when an object of structure or class type is created, and is typically used to initialize class members. Further we will discuss only classes, all the discussed can be applied to structures also, unless otherwise specified. The name of the constructor must match with the name of the class. The constructor has no return type (you can specify the type of void). In MQL5 constructors do not have any input parameters. Thus, each class can have only one constructor.
Class members: strings, dynamic arrays and objects that require initialization will be initialized anyway regardless of the constructor presence or absence.
A destructor is a special function that is called automatically when a class object is destroyed. The name of the destructor is written as a class name with a tilde (~). Strings, dynamic arrays and objects, requiring deinitialization, will be de-initialized anyway, regardless of the destructor presence or absence. If there is a destructor, these actions will be performed after calling the destructor.
Destructors are always virtual, regardless of whether they are declared with the virtual keyword or not.
Defining Class Methods
Class function-methods can be defined both inside the class and outside the class declaration. If the method is defined within a class, then its body comes right after the method declaration.
Example:
class CTetrisShape |
Functions with SetRightBorder(int border) on the Draw() are declared and defined directly inside the CTetrisShape class.
The CTetrisShape() constructor and methods CheckDown(int& pad_array[]), CheckLeft(int& side_row[]) and CheckRight(int& side_row[]) are only declared inside the class, but not defined yet. Definitions of these functions will be further in the code. In order to define the method outside the class, the scope resolution operator is used, the class name is used as the as the scope.
Example:
//+------------------------------------------------------------------+ |
Public, Protected and Private Access Modifiers
When developing a new class, it is recommended to restrict access to the members from the outside. For these purposes keywords private or protected are used. In this case, hidden data can be accessed only from function-methods of the same class. If the protected keyword is used, hidden data can be accessed also from methods of classes - inheritors of this class. The same method can be used to restrict the access to functions-methods of a class.
If you need to completely open access to members and/or methods of a class, use the keyword public.
Example:
class CTetrisField |
Any class members and methods declared after the specifier public: (and before the next access specifier) are available in any reference to the class object by the program. In this example these are the following members: functions CTetrisField(), Init(), Deinit(), Down(), Left(), Right(), Rotate() and Drop().
Any members that are declared after the access specifier to the elements private: (and before the next access specifier) are available only to members-functions of this class. Specifiers of access to elements always end with a colon (:) and can appear in the class definition many times.
Access to the members of the basis class can be redefined during inheritance in derived classes.
See also
© 2000-2010, MetaQuotes Software Corp.