少し基本的な質問ですが、決定的な答えを見つけるのに苦労しています。
初期化子リストは、メソッドでの代入を除いて、C ++でクラスフィールドを初期化する唯一の方法ですか?
間違った用語を使用している場合は、次のように言います。
class Test
{
public:
Test(): MyField(47) { } // acceptable
int MyField;
};
class Test
{
public:
int MyField = 47; // invalid: only static const integral data members allowed
};
編集:特に、構造体初期化子を使用して構造体フィールドを初期化するための優れた方法はありますか?例えば:
struct MyStruct { int Number, const char* Text };
MyStruct struct1 = {}; // acceptable: zeroed
MyStruct struct2 = { 47, "Blah" } // acceptable
class MyClass
{
MyStruct struct3 = ??? // not acceptable
};