Download MetaTrader 5

Variable declaration - what does *variableName mean?

To add comments, please log in or register
Do you have a trading signal? Publish it in your blog as well!
Keng Onn Wong
37
Keng Onn Wong 2011.02.03 17:15 

Hi,

 While looking through the code generated by the EA Wizard, I came across several variable declarations in this style:

CSignalCrossMA *signal=new CSignalCrossMA;

....

CMoneyFixedMargin *money=new CMoneyFixedMargin;

 ...

 I'm a Java (and a bit of Ruby) developer by profession so I roughly know my way about code and OO stuff, it's just that I'm not familiar with this type of *variableName type of variable declaration... ><

Would appreciate any pointers on this, thanks!

 Regards. 

Stephen Njuki
5179
Stephen Njuki 2011.02.03 23:11  
discrete:

Hi,

 While looking through the code generated by the EA Wizard, I came across several variable declarations in this style:

CSignalCrossMA *signal=new CSignalCrossMA;

....

CMoneyFixedMargin *money=new CMoneyFixedMargin;

 ...

 I'm a Java (and a bit of Ruby) developer by profession so I roughly know my way about code and OO stuff, it's just that I'm not familiar with this type of *variableName type of variable declaration... ><

Would appreciate any pointers on this, thanks!

 Regards. 

"Would appreciate any pointers on this, thanks!"

Indeed!

the asterisk is used in declaring a pointer. This is simply a variable whose declaration and parameters are done/ set programatically.

Keng Onn Wong
37
Keng Onn Wong 2011.02.04 19:35  
ssn:

"Would appreciate any pointers on this, thanks!"

Indeed!

the asterisk is used in declaring a pointer. This is simply a variable whose declaration and parameters are done/ set programatically.

Haha, thanks!

After some reading up on the subject, I think I've a clearer idea of pointers. However, the way it's used in the generated code is a bit different from what is mentioned on websites (e.g. this one) . In the generated code, it goes something like this: 

CSignalCrossMA *signal=new CSignalCrossMA;
if(signal==NULL)
{       //--- failed
        ...
        return(-2);
}
if(!ExtExpert.InitSignal(signal))
{       //--- failed
        return(-3);
}
signal.FastPeriod(Inp_Signal_CrossMA_FastPeriod);
signal.FastShift(Inp_Signal_CrossMA_FastShift);

Why can't a normal object declaration, i.e. "CSignalCrossMA signal=new CSignalCrossMA;" be used instead? It looks like all normal object calls and method calls after the pointer declaration...

Stephen Njuki
5179
Stephen Njuki 2011.02.04 19:43  
discrete:

Haha, thanks!

After some reading up on the subject, I think I've a clearer idea of pointers. However, the way it's used in the generated code is a bit different from what is mentioned on websites (e.g. this one) . In the generated code, it goes something like this: 

Why can't a normal object declaration, i.e. "CSignalCrossMA signal=new CSignalCrossMA;" be used instead? It looks like all normal object calls and method calls after the pointer declaration...

The reason must have something to do with memory allocation... i.e. whether memory is set aside after before the EA starts running or whether it is scavenged for while the EA is running...
Jin
1539
Jin 2011.02.05 08:33  

discrete,

Differently from Java or C#, you don't need to use new to create an instance of the object. Instead just declare it, and start to use:

 CSignalCrossMA signal;

 At the end of the function it will be destroyed automatically.

When you use pointers, you have to destroy them manually before the end of the function, otherwise they will continue in memory during runtime and every time you call the function, it will create new instances causing memory leak.

So, what's the advantage? The advantage is that you can have pointers as a return of a function, while you can't do the same with objects  (it would be destroy at the end of the function).

I hope that I have helped somehow. 

Jin 

Documentation on MQL5: Checkup / Point
  • www.mql5.com
Checkup / Point - Documentation on MQL5
To add comments, please log in or register