如何防止通过引用返回的私有结构发生突变

How to prevent mutation of private structures returned by reference

我有以下代码:

struct MyStruct {
    int a;
    int b;
};

class Foo {
public:
    MyStruct const &getValue() {
        return value;
    }

private:
    MyStruct value{10, 20};
};

int main() {
    Foo foo{};
    auto v = foo.getValue();
    v.a = 42; // compiles! it shouldn't!
}

我想 return 对私有成员的引用,但不允许调用者改变此引用的内容。仅仅将 return 类型声明为 MyStruct const & 是不够的,因为它可能将引用本身定义为 const,而不是对象引用指向。但是,将 return 类型声明为 MyStruct& const 失败并显示“'const' 限定符可能不适用于引用”。我相信我可能在这里遗漏了一些东西。

我可以通过对所有常量字段声明 MyStruct 来解决这个问题,但我想在我的 class 中自己对其进行变异——只是不允许调用者这样做。

这怎么可能?

Just declaring return type to be MyStruct const & is not enough, as it probably defines the reference itself to be const, not the object reference points to.

错了。无论如何都不能重新分配引用。这是 return 对不可变对象的引用的正确方法。然而,您实际上 复制 returned 对象:

auto v = foo.getValue();

然后修改副本,这是一个有效的操作。您可以将 v 声明为参考

auto &v = foo.getValue();

得到预期的编译器错误。 Demo

您的错误假设是 auto v 声明了引用。它没有,您得到了从 getValue().

返回的 MyStruct 的副本

您可以检查原始对象是否未发生变异:

int main() {
    Foo foo{};
    auto v = foo.getValue();
    v.a = 42; // compiles! it should!
    std::cout << foo.getValue().a; //prints 10
}

v 声明为引用类型不会编译

int main() {
    Foo foo{};
    auto& v = foo.getValue(); // const is deduced by auto
    v.a = 42; // error
}