Initializing 2D Array In Class Constructor in C++ -


i define 2d array in header file

char map[3][3]; 

how can initialize values in class constructor this

 map = {{'x', 'x', 'o'},        {'o', 'o', 'x'},        {'x', 'o', 'x'}}; 

firstly, there difference between assignment , initialization. op title initialization.

secondly, have not told if 2d array class member(static/non static) or namespace variable.

-since mentioned initializing in class constructor, assuming class non static member, because:

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

further, of c++03 member array cannot initialized in constructor initializer list case in op(not sure c++0x) though.

-if 2d array static member of class, should initialize did (with slight change), not in constructor. should done in enclosing namespace scope once , once in of translation units.

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

-alternatively, if 2d array namespace scope variable, definition of array should taken out of header file (unless static) cause redefinition error , defined , initialized once , once in translation unit as

char map[3][3] = {{'x', 'x', 'o'},         {'o', 'o', 'x'},         {'x', 'o', 'x'}};  

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -