Why can't I declare a reference to a mutable object? (“reference cannot be declared mutable”)

前端 未结 4 619
别那么骄傲
别那么骄傲 2021-02-05 04:04

Let\'s say we have a test.cpp as follows:

class A;

class B
{
    private:
        A mutable& _a;
};

Compilation:



        
4条回答
  •  误落风尘
    2021-02-05 05:04

    References can only be assigned when constructing an object, and cannot be modified thereafter. Thus making them mutable would have no meaning, which is why the standard disallows it.

提交回复
热议问题