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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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));
}

5 Comments

memcpy is dangerous here. If someone in the future changes the dimensions or type of one array without reflecting those changes in the other...
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.
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])))
Good point Christoph, I've done that before. I hate #defining count() every time I need it - should have been a built-in feature.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.