class ConstRef { public: ConstRef(int ii); private: int i; const int ci; int &ri; }; //ConstRef::ConstRef(int ii) //{ // i = ii; // ok // ci = ii; // error:cannot assign to a const // ri = ii; // error:ri was never initialized //} // ok:explicitly initialize reference and const members ConstRef::ConstRef(int ii) :i(ii), ci(ii), ri(ii){};
只能通过构造函数的初始化列表来为const, reference or of a class type that does not have a default-constructor进行初始化。
时间: 2024-12-29 11:41:24