class MyClass {
public:
void doSomething() const {
// 不能修改成员变量
}
};
class MyClass {
public:
MyClass operator+(const MyClass& other) const {
// 返回一个新的对象,不会修改当前对象
}
};
class MyClass {
public:
int getValue() const {
return value;
}
private:
int value;
};
void processObject(const MyClass& obj) {
// obj.doSomething(); // 错误,const对象不能调用非const函数
int value = obj.getValue(); // 正确,调用const函数
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>