I've got the following code:
/* 1 */ class Market {
/* 2 */ public:
/* 3 */
/* 4 */ double foo;
/* 5 */
/* 6 */ /*
/* 7 */ * Constructor:
/* 8 */ */
/* 9 */ Market() {
/* 10 */ foo = 2;
/* 11 */ }
/* 12 */ };
/* 13 */
/* 14 */ Market market; // GLOBALLY VISIBLE VAR
/* 15 */
/* 16 */ void OnInit() { // OnInit event-process
/* 17 */ market = new Market(); // <------------------- 17,10
/* 18 */ Print(market.foo);
/* 19 */ }
/* 20 */
/* 21 */ void OtherFunc() {
/* 22 */ Print(market.foo);
/* 23 */ }
However when I'm trying to compile, I've got the following error:
test.mq4(17,10) :
error 280:
'=' - object required
Basically I'm planing to create an object instance and receive a pointer to it, so I can use it globally across the code.
I've checked the MQL4
docs page about classes, but it didn't help me.
I've also tried to assign the class as:
Market *market = new Market();
and then I declared that variable in the first line as:
Market market;
but then I've got this warning:
test.mq4(17,11) :
warning 62:
declaration of 'market' hides global declaration at line 14
and the file compiles, but I don't want to hide the global declaration (since it won't be available in the other functions), I want to assign an object instance into the global declaration, so I can print the object's variable across the whole code.
How do I do that?
MQL4
First advice, never rely on web-sources, always check the auto-updated IDE
-Help
. Even this does not contain a 100% correct text / code-samples, but it is by far closer to the actual compiler-grammar, than the obsoleting web texts.
Next advice, always remember MQL4
is not a C
language - strange differences may kill otherwise smart idea.
Object Create Operator
new
Thenew
operator automatically creates an object of a corresponding size, calls the object constructor and returns adescriptor
of created object. In case of failure, the operator returns anull descriptor
that can be compared with theNULL
constant.
Thenew
operator can be applied only toclass
objects. It can't be applied to structures.
Object Pointers
InMQL4
, there is a possibility to dynamically create objects of complex type. This is done by thenew
operator, which returns adescriptor
of the created object.Descriptor
is 8 bytes large. Syntactically, object descriptors inMQL4
are similar to pointers inC++
.
Examples:
MyObject* hobject= new MyObject();
In contrast toC++
, thehobject
variable from example above is not a pointer to memory, but rather an objectdescriptor
. Furthermore, inMQL5
all objects in function parameters must be passed by reference.
Using the above, hobject
ought be visible on a global-scope, supposing it's declaration was in due place, before any system-event-handler / user defined code started ( and sure, on condition there is no name-masking taking place inside any lower-level scope-range of the code ).
After an exposure to a bit more than a few hundreds man*years
of MQL4
hands-on experience, all advanced solutions started to get designed as a distributed framework, rather than have to remain as victims of language-irregularities and syntax-creeps. One may consider this as a bit safer alternative than to invest some remarkable amount of software engineering into ecosystem, that changes faster, than an economy / performance of a repetitive code-base re-engineering might manage to keep the pace of the moving sands.
Anyway : Welcome to the Wild Worlds of
MQL4