假设我有以下 class X
我想在其中返回对内部成员的访问权限:
class Z
{
// details
};
class X
{
std::vector<Z> vecZ;
public:
Z& Z(size_t index)
{
// massive amounts of code for validating index
Z& ret = vecZ[index];
// even more code for determining that the Z instance
// at index is *exactly* the right sort of Z (a process
// which involves calculating leap years in which
// religious holidays fall on Tuesdays for
// the next thousand years or so)
return ret;
}
const Z& Z(size_t index) const
{
// identical to non-const X::Z(), except printed in
// a lighter shade of gray since
// we're running low on toner by this point
}
};
两个成员函数 X::Z()
和 X::Z() const
在大括号内具有相同的代码。这是重复的代码 ,可能会导致逻辑复杂的长函数出现维护问题。
有没有办法避免这种代码重复?
原文由 Kevin 发布,翻译遵循 CC BY-SA 4.0 许可协议
是的,可以避免代码重复。您需要使用 const 成员函数来拥有逻辑并让非常量成员函数调用 const 成员函数并将返回值重新转换为非常量引用(如果函数返回指针,则为指针):
注意: 重要的是 不要 将逻辑放在非常量函数中并且让常量函数调用非常量函数 - 这可能会导致未定义的行为。原因是常量类实例被强制转换为非常量实例。非常量成员函数可能会意外修改类,C++ 标准规定这将导致未定义的行为。