MQL4 Reference Language Basics Object-Oriented Programming Static Members of a Class
Static members of a Class/StructureStatic MembersThe members of a class can be declared using the storage class modifier static. These data members are shared by all instances of this class and are stored in one place. Non-static data members are created for each class object variable. The inability to declare static members of a class would have led to the need to declare these data on the the global level of the program. It would break the relationship between the data and their class, and is not consistent with the basic paradigm of the OOP - joining data and methods for handling them in a class. The static member allows class data that are not specific to a particular instance to exist in the class scope. Since a static class member does not depend on the particular instance, the reference to it is as follows:
where class_name is the name of the class, and variable is the name of the class member. As you see, to access the static member of a class, context resolution operator :: is used. When you access a static member within class methods, the context operator is optional. Static member of a class has to be explicitly initialized with desired value. For this it must be declared and initialized in global scope. The sequence of static members initialization will correspond to the sequence of their declaration in global scope. For example, we have a class CParser used for parsing the text, and we need to count the total number of processed words and characters. We only need to declare the necessary class members as static and initialize them at the global level. Then all instances of the class will use common counters of words and characters.
A static class member can be declared with the const keyword. Such static constants must be initialized at the global level with the const keyword:
Pointer thisThe keyword this denotes an implicitly declared pointer to itself to a specific instance of the class, in the context of which the method is executed. It can be used only in non-static methods of the class. Pointer this is an implicit non-static member of any class. In static functions you can access only static members/methods of a class. Static MethodsIn MQL4 member functions of type static can be used. The static modifier must precede the return type of a function in the declaration inside a class.
A method with the const modifier is called constant and cannot modify implicit members of its class. Declaration of constant functions of a class and constant parameters is called const-correctness control. Through this control you can be sure that the compiler will ensure the consistency of values of objects and will return an error during compilation if there is something wrong. The const modifier is placed after the list of arguments inside a class declaration. Definition outside a class should also include the const modifier:
An additional argument in favor of using the constancy control is the fact that in this case, the compiler generates a special optimization, for example, places a constant object in read-only memory. A static function cannot be determined with the const modifier, because this modifier ensures the constancy of the instance members when calling this function. But, as mentioned above, the static function cannot access non-static class members. See also Static Variables, Variables, References. Modifier & and Keyword this |