Constants in Classes

 

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?

 
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;
 

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!

Reason: