此外,const 物件 只能引用 const 函式。
class A
{
private:
int number;
public:
A(int value){number=value;};
void const_f(void) const {/*number=1;*/}; // 不能對成員變數賦值
void nonconst_f(void){number=1;};
};
int main()
{
const A a(1);
a.const_f();
a.nonconst_f(); // 編譯錯誤: const 物件只能引用 const 函式
return 0;
}
沒有留言:
張貼留言