how to initialize member array in Class Constructor method? - page 2

 
lippmaje:

If you want a non-dynamic array you have to make sure to specify exactly the same number of elements that's used in the initializer list, otherwise you'll get this error.


To avoid that hassle I suggest to always go with dynamic arrays when it comes to initialize it by a list.


The advantage is that you don't have to count your initializers and adapt that array size everytime you change that list.

The disadvantage is that a dynamic array uses up more memory - 52 bytes - for the dynamic array properties. I guess you can live with this in most of the cases.

Thanks for detailed explanation lippmaje.

Well while making it static, defining ArrayResize was the problem in my mind (as I did not knew, how I will for a member Array).

now explanation has made everything crystal clear.

Thanks a lot once again.

 
lippmaje:


Hi lippmaje

Can you suggest some structure and how to fill it & call it. The object is to store static Camarilla Pivot Levels (R5,R4,R3,R2,R1,PP,S1,S2,S3,S4 and S5) and change dynamically level values every new day start.

In simple language, I am looking to create Column[1] as List of Pivot Levels (to be able to select level) and Colum[2] for 'level values' which shall change everyday. No need to keep history data.

It is some how similar to Fibonacci structure we used above, but I am finding difficult to handle string values of PivotLevels.

Thanks in advance for you support and help.

 
William Roeder #:
  1. You didn't declare your constructor in the class.

  2. The initialization list only works when you declare an array. You can not use it at runtime.

  3. Options:
    1. Make it a static member.

    2. Make a array in the constructor and copy to the member array.
    3. Do it the hard way.

Hi William

I have a similar problem, and wish to follow your second option but am getting stuck at the missing default value for the passed array:

class CValue{
   private:
      int      m_Dec;
      string   m_Binary;
      bool     m_BoolArray[];
      int      m_Exponent;
   public:
      CValue(int aDec=NULL, string aBinary="", bool &aBoolArray[]=???, int aExponent=NULL) {
         // Happy to loop through aBoolArray here to populate m_BoolArray (although if you have a better idea please let me know)
         Init(aDec, aBinary, aExponent);
      }

      void Init(int aDec=NULL, string aBinary="", int aExponent=NULL) {
         // Set parameters
         m_Dec       =aDec;
         m_Binary    =aBinary;
         m_Exponent  =aExponent;
      }
};

Please help! The class is to enable the representation of an array of boolean values as a decimal (via binary) number for optimisation of large (variable) numbers of boolean parameters

 
", bool &aBoolArray[]=???, in
You can't default a reference. Arrays must be passed by reference. Write two constructors.
 
William Roeder #:
You can't default a reference. Arrays must be passed by reference. Write two constructors.

I am so sorry, but I do not understand your last sentance - the first I suspected, the second I knew. But I still do not understand how to make this code reusable in terms of being able to define a class array without defining a global array prior to instantiating the object and iterating through it in the constructor... or is this what you meant i.e. 

class CValue{
   private:
      int      m_Dec;
      string   m_Binary;
      bool     m_BoolArray[];
      int      m_Exponent;
   public:
      CValue(int aDec=NULL, string aBinary="", int aExponent=NULL) {
        ArrayResize(m_BoolArray,ArraySize(gArray)); 
        for(i=0;i<ArraySize(gArray);i++{
          m_BoolArray[i] = gArray[i];
        }
        Init(aDec, aBinary, aExponent);
      }

      void Init(int aDec=NULL, string aBinary="", int aExponent=NULL) {
         // Set parameters
         m_Dec       =aDec;
         m_Binary    =aBinary;
         m_Exponent  =aExponent;
      }
};

bool gArray[];
// initialise gArray and assign values here

CValue value1();

also, should the Init declaration start with 

void Init(int aDec=NULL, string aBinary="", int aExponent=NULL)

or 

void Init(int aDec, string aBinary, int aExponent)

given that default values are given in the declaration of the constructor... I suppose they should remain in the Init declaration to allow reuse of the function after the Class instance is loaded?

 

 
William Roeder #: You can't default a reference. Arrays must be passed by reference. Write two constructors.
Andrew Thompson #: I am so sorry, but I do not understand your last sentance - t
      CValue(int aDec, string aBinary, bool &aBoolArray[], int aExponent=NULL) {  // use array to initialize class

      CValue(int aDec=NULL, string aBinary="", int aExponent=NULL) {              // no array passed
        bool aBoolArray[]={ 0, 1, …];                                             // default array.
Reason: