总忘记
Matrix & L(Index blockId) { return *L_[blockId]; }
47 Matrix & D(Index blockId) { return *D_[blockId]; }
48 Matrix & U(Index blockId) { return *U_[blockId]; }
49 Matrix & L(Index blockId) const { return *L_[blockId]; }
50 Matrix & D(Index blockId) const { return *D_[blockId]; }
51 Matrix & U(Index blockId) const { return *U_[blockId]; }
如果没有const 版本test(const matrix & m)
{
m.D(i);
}
会报错
为什么呢?
test 中const 目的是告诉编译器不改变变量值,而如果没有const 版本,则编译器调用发现D(), U(), L()都有可能改变变量至,所以拒绝调用,报错
本文探讨了C++中常量引用结合成员函数的使用场景,特别是当函数声明为const时如何避免修改对象状态,并解释了如果没有提供const版本的成员函数可能会导致的编译错误。
1024

被折叠的 条评论
为什么被折叠?



