修改構造函數C++標準中的const?我正在修改我的struct
刪除固定值(默認成員初始值設定器)稍後在構造函數時設置它,但我忘記刪除const
關鍵字,並在稍後通知它。令我驚訝的是我沒有得到編譯錯誤,它只是工作正常,但對於測試用例2它給了一個編譯器。他們有什麼不同?是否在構造函數C++標準中修改const?
測試用例1:
struct A
{
const int x = 2;
A()
: x(3)
{
}
};
測試用例2:
struct A
{
const int x = 2;
A()
{
x = 3; // compile error! error: read-only variable is not assignable
}
};
請參閱[這個問題](http://stackoverflow.com/questions/355258/why-must-const-members-be-intialized-in-the-constructor-initializer-rather-than)。這似乎相關。 – blazs