当函数后面的“const”关键字有什么作用?

发布于 2024-10-09 03:12:12 字数 317 浏览 9 评论 0原文

可能的重复:
a 的含义是什么const 位于成员函数末尾?

我见过一些具有类似内容的类。

void something() const;

const 是什么意思?

Possible Duplicate:
What is the meaning of a const at end of a member function?

I have seen some classes that have something like this.

void something() const;

What does const means?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

囚你心 2024-10-16 03:12:12

除了 SRM 所说的(该函数不能更改类的任何成员(这并不完全正确,例如在 mutable 成员的情况下,它仍然可以更改)),这意味着您可以对 const 的事物调用此成员函数。所以如果你得到一个 Foo const& foo 作为参数,您只能调用像问题中那样声明为 const 的成员。

In addition to what SRM said (that the function cannot alter any members of the class (which is not exactly true, e.g. in the case of mutable members, which still can be altered)), it means that you can call this member function on things that are const. So if you get a Foo const& foo as a parameter, you can only call members that are declared const like in your question.

会傲 2024-10-16 03:12:12

这意味着某物不能修改类的成员变量。
该规则的例外情况是,如果存在使用 mutable 关键字声明的成员变量。

例如,假设您有一个 std::map< int, int > var 成员变量和执行以下操作的方法:

int Class::method () const {
    if (var.find (42) != var.end ()) {
        return var[42];
    }
    return 0;
}

如果 42 不在容器中,则不会编译,因为 var[42] 会修改 var。将 var 声明为 mutable 可以让您通过编译。

It means that something may not modify member variables of the class.
The exception to the rule is if there are member variables declared with the mutable keyword.

For example, suppose you have a std::map< int, int > var member variable and a method that does the following:

int Class::method () const {
    if (var.find (42) != var.end ()) {
        return var[42];
    }
    return 0;
}

That will not compile since var[42] modifies var if 42 is not in the container. Declaring var as mutable allows you to pass compilation.

蓝天白云 2024-10-16 03:12:12

这意味着该函数不会修改任何成员变量。更准确地说,这意味着该函数不能更改该类的任何非静态或不可变成员变量

It means the function will not modify any member variables. To be more precise, it means the function cannot alter any non-static or immutable member variables of that class

喜你已久 2024-10-16 03:12:12

绝对准确地说,它使函数中的隐式 this 指针成为指向 const 对象的指针。

来自9.2.1“this指针”

类 X 的成员函数中 this 的类型是 X*。如果成员函数声明为const,则this的类型为const X*,如果成员函数声明为volatile,则this的类型是 volatile X*,如果成员函数声明为 const volatile,则 this 的类型为 const volatile X*

所有其他行为(您不能修改对象成员、您可以使用 const 对象调用函数等)都源于此。

To be absolutely precise - it makes the implicit this pointer in the function a pointer to a const object.

From 9.2.1 "The this pointer""

The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

All the other behaviors (that you can't modify the object members, that you can call the function using a const object, etc) fall from that.

执着的年纪 2024-10-16 03:12:12

它声明该函数不能更改任何数据。使其成为只读函数。

it declares that this function can not change any data. Makes it a Read Only Function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文