3

I define a 2D array in my header file

char map[3][3];

How can I initialize the values in the class constructor like this

 map = {{'x', 'x', 'o'},
       {'o', 'o', 'x'},
       {'x', 'o', 'x'}};
2
  • Did you actually try your code? Commented Oct 7, 2010 at 4:43
  • 1
    Is this going to be a static constant array, static variable array, or class member? Commented Oct 7, 2010 at 5:40

2 Answers 2

4

Firstly, there is a difference between assignment and initialization. The OP title is about initialization.

Secondly, you have not told us if your 2D array is a class member(static/non static) or a namespace variable.

-Since you mentioned about initializing it in the class constructor, I am assuming that it is a class non static member, because:

$12.6.2/2 - "Unless the mem-initializer-id names the constructor’s class, a non-static data member of the constructor’s class, or a direct or virtual base of that class, the mem-initializer is ill-formed."

Further, as of C++03 the member array cannot be initialized in the constructor initializer list for the case in OP(not sure about C++0x) though.

-If your 2D array is a static member of your class, you should initialize it as you did (with a slight change), but not in the constructor. This should be done in the enclosing namespace scope once and only once in any of the translation units.

char (A::map)[3][3] = {{'x', 'x', 'o'}, 
       {'o', 'o', 'x'}, 
       {'x', 'o', 'x'}};

-Alternatively, if your 2D array is a namespace scope variable, the definition of the array should be taken out of the header file (unless it is also static) as it will cause a redefinition error and be defined and initialized once and only once in any translation unit as

char map[3][3] = {{'x', 'x', 'o'}, 
       {'o', 'o', 'x'}, 
       {'x', 'o', 'x'}}; 
Sign up to request clarification or add additional context in comments.

2 Comments

Well, since initializing an array in an initialization list is impossible, it must be done by way of assignment in the body of the constructor. So I would call that "initializing" even if it wasn't done upon creation of the variable.
As Scott Myers says, assignment is better than initialization for built in data types. Much easier to mantain and less error prone !!
1
memcpy(map,"xxoooxxox",9);

or

char tmp[3][3] =
    {{'x','x','o'},
     {'o','o','x'},
     {'x','o','x'}};
memcpy(map,tmp,9);

6 Comments

char tmp[] = "xxoooxxox"; char* map[3] = { tmp + 0, tmp + 3, tmp + 6 };
@rwong as soon as the tmp array is destroyed your map will have invalid pointers.
@rwong You just created a new array. This will have no effect on the array that's part of his class.
@Michael: my answer was responding to the earlier version of PigBen's answer, which has been edited away. What I wanted to point out is that taking the address of tmp from char tmp[3][3] would not give a pointer to char, it would give a pointer to pointer to char.
Sorry, I didn't read question careful enough. None of the code works. Chubsdad had given the answer.
|

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.