为什么在C++11中const成员函数能够修改对象的可变数据成员?

huangapple go评论79阅读模式
英文:

Why can a const member function modify an object's mutable data member in C++11?

问题

为什么和如何可以使一个 const 成员函数修改对象的 mutable 数据成员?

成员函数使用 this 指针来修改对象的数据成员。const 成员函数的 this 指针是指向常量的指针,而不是普通的数据成员。那么为什么总是可以修改对象上标记为 mutable 的数据成员,编译器是如何实现的呢?

英文:

Why and how can a const member function modify an object's mutable data member?

Member functions use the this pointer to modify the data members of an object. The this pointer of a const member function is a pointer to a constant, not a normal data member. So why is it always possible to modify the data members marked mutable of an object, and how does the compiler implement it?

答案1

得分: 7

mutable成员变量的整个目的是它们可以通过const实例进行修改。因此,thisconst与修改mutable成员变量时无关,就像读取成员变量时无关一样。

英文:

The entire point of mutable member variables is that they can be modified via a const instance. So the fact that this is const has no relevance when modifying a mutable member variable--just as it has no relevance when reading a member variable.

答案2

得分: 3

在询问编译器如何处理 mutable 之前,你应该先了解编译器如何处理 const

正如其名称所示,mutable 只是另一个限定符(或者更确切地说是存储类别说明符),它与 const 的处理方式有关。它表示一个 mutable 成员变量可以在声明为 const 的情况下进行修改。它也可以通过对包含对象的 const 引用(或指针)进行修改。它与成员函数没有直接关系。

请注意,mutable 成员变量不能通过直接的 const 引用(或指针)进行修改。

在考虑对符号进行变异操作时,编译器只是根据上述规则检查符号是否可以在这个上下文中进行变异。

示例

struct S {
    mutable int i;
};

int main() {
    S const object{0};
    object.i = 1; // 可以,因为 object.i 声明为 `mutable`,即使 object 是 `const`
    S const& ref = object;
    ref.i = 2; // 可以,因为 ref 引用了具有 `mutable` 成员 `i` 的对象

    int const& direct_ref = object.i;
    direct_ref = 3; // 错误,因为 direct_ref 直接引用了一个 `const` 符号
}
英文:

> how does the compiler implement it?

Before asking how the compiler handles mutable, you should ask how the compiler handles const.

As the name implies, mutable is just another qualifier (or rather a storage class specifier) that interacts with how const is handled. It simply means that a mutable member variable can be modified, even if the containing object is declared const. It can also be modified through a const reference (or pointer) to the containing object. It does not directly relate to member functions.

Note that a mutable member variable cannot be modifier through a direct const reference (or pointer).

When looking at a mutating operation on a symbol, the compiler simply checks whether the symbol can be mutated in this context according to the rules above.

Example:

struct S {
    mutable int i;
};

int main() {
    S const object{0};
    object.i = 1; // ok because object.i is qualified `mutable`, even if object is `const`
    S const& ref = object;
    ref.i = 2; // ok because ref refers to an object that has a `mutable` member `i`

    int const& direct_ref = object.i;
    direct_ref = 3; // error because direct_ref directly refers to a `const` symbol
}

huangapple
  • 本文由 发表于 2023年5月28日 21:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定