Variable declaration - what does *variableName mean?

 

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. 

 
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.

 
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...

 
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...
 

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