常量引用如何工作?

How do constant references work?

最近我一直在学习 C++ 中的良好编程习惯,发现许多程序通过引用将对象传递给函数,这样就不会创建多个实例。我还了解到传递常量引用可以防止原始对象被修改,但是我不明白这是如何工作的。常量引用不应该创建一个新实例,因为原始对象不能通过引用修改但引用仍然可以像单独的对象一样使用吗?我很确定这不是它的工作原理,但是它是如何工作的?我错过了什么吗?

常量引用(const&)类似于指向常量对象的指针。您可以通过引用阅读它,但不能修改它。其他人,持有 non-const 参考 can 仍然可以修改它。

I have also learned that passing a constant reference prevents the original object from being modified [...]

不完全是。您不能通过 修改对象 const &。换句话说,您具有只读访问权限。但是没有什么可以在本质上阻止其他具有读写访问权限的代码(例如,被引用对象的原始所有者)修改它。您在设计时确实需要小心,这样的变化不会让您感到意外。

Shouldn't a constant reference create a new instance because the original object cannot be modified through the reference but the reference can still be used like a separate object?

最好称它为对常量对象的引用。这使得它的工作原理更加清晰。反过来调用它只会让人感到困惑,因为 any 引用是常量(这意味着你不能让它在初始化后引用另一个对象)。

所以 对常量对象的引用 只是现有对象的附加名称(如非常量引用),限制是此名称仅允许 正在从现有对象读取

这意味着通过对常量对象的引用您可以:

  • 只读取对象的成员变量,但不赋值给它们,除非成员被标记为mutable
  • 只调用标记为const
  • 的对象的方法

示例:

struct Foo
{
    int a;
    mutable int b;

    void SetA( int newA ) { a = newA; }
    int GetA() const      { return a; }
};

void DoSomething( const Foo& f )
{
    // Here, f is just another name for foo, but it imposes some restrictions:
    f.a = 42;          // compiler error, can't modify member!
    f.SetA( 42 );      // compiler error, can't call non-const method!
    int x = f.a;       // OK, reading is allowed.
    f.b = 42;          // OK, because b is marked as mutable
    int y = f.GetA();  // OK, because GetA() is marked as const 
}

int main()
{
    Foo foo;
    DoSomething( foo );
}