程式設計師可能想知道為什麼類別資料成員不能使用直接初始化語法賦值,類似於本地初始化語法變數可以。考慮以下範例:
class test { public: void fun() { int a(3); std::cout << a << '\n'; } private: int s(3); // Compiler error: Why??? };
編譯此程式碼時,會出現錯誤:
11 9 [Error] expected identifier before numeric constant 11 9 [Error] expected ',' or '...' before numeric constant
struct S { int i(x); // data member with initializer // ... static int x; }; struct T { int i(x); // member function declaration // ... typedef int x; };
struct S { int i(j); // ill-formed...parsed as a member function, // type j looked up but not found // ... static int j; };
struct S { int i(x); // unabmiguously a data member int j(typename y); // unabmiguously a member function };
以上是為什麼不能使用直接初始化語法在 C 中初始化類別資料成員?的詳細內容。更多資訊請關注PHP中文網其他相關文章!