隐含的this指针

2021-02-17  本文已影响0人  陈成_Adam

编译错误的示例

class Car {
public:
 const int &weight()
 {
 return m_weight;
 }
private:
 int m_weight;
};
int main(int argc, char *argv[])
{
 const Car car;
 int weight = car.weight();
 return 0;
}

编译后会出现以下错误:

main.cpp:15: error: C2662: “const int &Car::weight(void)”: 不能将“this”指针从“const Car”转换为“Car &”

为什么会这样?

怎么做?

总结

转载自:https://cloud.tencent.com/developer/article/1464343

上一篇 下一篇

猜你喜欢

热点阅读