4

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

ephemient
  • 198,619
  • 38
  • 280
  • 391
Milan
  • 15,389
  • 20
  • 57
  • 65

3 Answers3

10

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.

ephemient
  • 198,619
  • 38
  • 280
  • 391
Rob K
  • 8,757
  • 2
  • 32
  • 36
7

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.

Perchik
  • 5,262
  • 1
  • 19
  • 22
5

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));
}
Community
  • 1
  • 1
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • memcpy is dangerous here. If someone in the future changes the dimensions or type of one array without reflecting those changes in the other... – Mark Ransom Feb 06 '09 at 16:24
  • True, but you'd have the same problem with any other solution. You could easily throw in an assert(sizeof(wa) == sizeof(default_wa)) here to protect against that in this case. – Adam Rosenfield Feb 06 '09 at 16:29
  • 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])))` – Christoph Feb 06 '09 at 16:41
  • Good point Christoph, I've done that before. I hate #defining count() every time I need it - should have been a built-in feature. – Mark Ransom Feb 06 '09 at 19:06
  • 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. – Mark Ransom Feb 06 '09 at 19:09