Initializing a 2D Array in Class Designer in C ++

I define a 2D array in the header file

char map[3][3];

How can I initialize the values ​​in the class constructor as follows

 map = {{'x', 'x', 'o'},
       {'o', 'o', 'x'},
       {'x', 'o', 'x'}};
+3
source share
2 answers

Firstly, there is a difference between assignment and initialization. The OP header contains initialization.

Secondly, you did not tell us whether your 2D array is a member of a class (static / non-static) or a namespace variable.

- Since you mentioned its initialization in the class constructor, I assume that this is a non-stationary member of the class, because:

$12.6.2/2 - " mem-initializer-id , , mem- ".

, ++ 03, OP ( ++ 0x).

- 2D- , , ( ), . .

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

-, 2D- , ( ),

char map[3][3] = {{'x', 'x', 'o'}, 
       {'o', 'o', 'x'}, 
       {'x', 'o', 'x'}}; 
+5
memcpy(map,"xxoooxxox",9);

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

Source: https://habr.com/ru/post/1768359/


All Articles