C/C++ private array initialization in the header file

22,507

Solution 1

If it can be static, you can initialize it in your .cpp file. Add the static keyword in the class declaration:

class Cal {
    private:
        static int wa[2][2];
    public:
        void do_cal();
};

and at file scope in the .cpp file add:

#include "Cal.h"
int Cal::wa[2][2] = { {5,2}, {7,9} };
void Cal::do_cal() {
   print(wa) // where print just itterates and prints the elements in wa
}

If you never change it, this would work well (along with making it const). You only get one that's shared with each instance of your class though.

Solution 2

You cannot initialize array elements in a class declaration. I recently tried to find a way to do just that. From what I learned, you have to do it in your initialize function, one element at a time.

Cal::Cal{
   wa[0][0] = 5;
   wa[0][1] = 2;
   wa[1][0] = 7;
   wa[1][1] = 9;
}

It's possible (and probable) that there's a much better way to do this, but from my research last week, this is how to do it with a multi dimensional array. I'm interested if anyone has a better method.

Solution 3

You can't do it easily. If you don't want to specify each element individually like in Perchik's answer, you can create one static array and memcpy that (which will probably be faster for non-trivial array sizes):

namespace
{
    const int default_wa[2][2] = {{5, 2}, {7, 9}};
}

Cal::Cal
{
    memcpy(&wa[0][0], &default_wa[0][0], sizeof(wa));
}
Share:
22,507
Milan
Author by

Milan

Updated on August 31, 2020

Comments

  • Milan
    Milan over 3 years

    I have a class called Cal and it's .cpp and .h counterpart

    Headerfile has

    class Cal {
        private:
            int wa[2][2];
    
        public:
            void do_cal();
    };
    

    .cpp file has

    #include "Cal.h"
    void Cal::do_cal() {
       print(wa) // where print just itterates and prints the elements in wa
    }
    

    My question is how do I initialize the array wa ? I just can't seem to get it to work.

    I tried with :

    int wa[2][2] = {
                    {5,2},
                    {7,9}
                   };
    

    in the header file but I get errors saying I cant do so as it's against iso..something.

    Tried also to initialize the array wa in the constructor but that didnt work either.. What am I missing ?

    Thanks

  • Mark Ransom
    Mark Ransom about 15 years
    memcpy is dangerous here. If someone in the future changes the dimensions or type of one array without reflecting those changes in the other...
  • Christoph
    Christoph about 15 years
    You can make sure that the array sizes always match if you define them like this: int default_wa[][2] = {{ 5, 2 }, { 7, 9 }}; int wa[count(default_wa)][count(default_wa[0])]; with #define count(ARRAY) ((sizeof (ARRAY))/(sizeof (ARRAY[0])))
  • Mark Ransom
    Mark Ransom about 15 years
    Should also point out that you can use a static within the constructor, rather than putting the defaults in a namespace outside. But only if you don't need an inline constructor. It enhances the readability.