Constants in Classes

Back to topics list To post a new topic, please log in or register
avatar
30
dennisj2 2015.02.15 07:24
 

Hi all,

Having a bit of trouble with the OOP implementation - I will say this first, I am a noob when it comes to OOP.

I want to define the following array as a global constant:

private:

    static const double FibonacciLevels[6] = {0.0,23.6,38.2,50.0,61.8,100.0};

Clearly, these values are immutable and, when defined using non-OOP MQL4, this is a standard definition for pre-loading an array.

Not a big deal, I can always define a method called in the constructor to load the array - but, this is tedious and as this could be 1 of many pre-defined arrays used by the class, I would write a method to load each array individually? There must be a better, accepted approach.

Hints?




avatar
1109
Ovo 2015.02.15 08:58 #
 
dennisj2:

Hi all,

Having a bit of trouble with the OOP implementation - I will say this first, I am a noob when it comes to OOP.

I want to define the following array as a global constant:

Clearly, these values are immutable and, when defined using non-OOP MQL4, this is a standard definition for pre-loading an array.

Not a big deal, I can always define a method called in the constructor to load the array - but, this is tedious and as this could be 1 of many pre-defined arrays used by the class, I would write a method to load each array individually? There must be a better, accepted approach.

Hints?

Implementation in MQL is a bit inconvenient.This is what doc reads. I did not try with arrays though. 

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:

//+------------------------------------------------------------------+
//| Class "Stack" for storing processed data                         |
//+------------------------------------------------------------------+
class CStack
  {
public:
                     CStack(void);
                    ~CStack(void){};
...
private:
   static const int  s_max_length; // Maximum stack capacity
  };
 
//--- Initialization of the static constant of the CStack class
const int CStack::s_max_length=1000;

 

avatar
30
dennisj2 2015.02.15 19:09 #
 

Far out! Thanks.

public:

     static const double FibonacciLevels[6];
  };
  
const double  CPolyRegression::FibonacciLevels[6]={0.0,23.6,38.2,50.0,61.8,100.0};


Works like a champ!



Back to topics list  

To add comments, please log in or register