1行で何を意味するのか、質問はあまり明確ではないので、提案を提供し始めます。
集計の初期化を使用します。
board fields[10][10] = {
{ {"ExampleName","This is an example",3,4,5,6}, // Element 0,0
{"ExampleName","This is an example",3,4,5,6}, // Element 0,1
// ...
},
{ {"ExampleName","This is an example",3,4,5,6}, // Element 1,0
// ...
}
};
これは取得できるワンライナーに最も近いもので、有効な C++ です (すべてのバリアントで、これがboard
集約であるため、集約でboard[10][10]
もあります) が、読みにくい場合があります。
次のステップは、(配列ではなく) 各要素を初期化することです。C++ 11 では、提案したのと同じタイプの初期化を使用できます。C++03 では、コンストラクターを介してこれを行う必要があります (注: これにより型のプロパティが変更されるため、配列の既定のコンストラクターを作成する必要があります)。
struct board {
string name;
string desc;
int nval;
int sval;
int eval;
int wval;
board() {} // [*]
board( const char* name, const char* desc, int n, int s, int e, int w )
: name(name), desc(desc), nval(n), sval(s), eval(e), wval(w)
{}
};
board fields[10][10]; // [*] this requires the default constructor [*]
fields[5][6] = board("ExampleName","This is an example",3,4,5,6);
または関数を介して:
board create_board( const char* name, const char* desc, ... ) {
board res = { name, desc, ... };
return res;
}
どちらの場合も、配列の初期化中に配列内の要素が初期化され、新しいオブジェクトがそれらの上にコピーされることに注意してください。