“const”是什么意思?在函数声明的末尾?

发布于 2024-09-07 11:03:24 字数 150 浏览 4 评论 0原文

我有一本书,上面写着这样的话:

class Foo 
{
public:
    int Bar(int random_arg) const
    {
        // code
    }
};

这是什么意思?

I got a book, where there is written something like:

class Foo 
{
public:
    int Bar(int random_arg) const
    {
        // code
    }
};

What does it mean?

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

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

发布评论

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

评论(6

自此以后,行同陌路 2024-09-14 11:03:24

“const 函数”(在函数声明后用关键字 const 表示)会使此类函数更改类的数据成员时出现编译器错误。但是,在函数内部读取类变量是可以的,但在该函数内部写入会产生编译器错误。

思考此类“const 函数”的另一种方式是将类函数视为采用隐式 this 指针的普通函数。因此,方法 int Foo::Bar(int random_arg) (末尾没有 const)会产生类似 int Foo_Bar(Foo* this, int random_arg) 的函数,以及诸如 Foo f; 之类的调用f.Bar(4) 在内部将对应于类似 Foo f; 的内容。 Foo_Bar(&f, 4).现在在末尾添加 const (int Foo::Bar(int random_arg) const) 就可以理解为带有 const this 指针的声明: int Foo_Bar(const Foo* this, int random_arg)。由于在这种情况下 this 的类型是 const,因此无法修改数据成员。

可以放宽不允许函数写入类的任何变量的“const 函数”限制。为了允许某些变量即使在函数被标记为“const 函数”时也可写,这些类变量用关键字 mutable 进行标记。因此,如果一个类变量被标记为可变的,并且“const 函数”写入该变量,那么代码将干净地编译并且该变量可以更改。 (C++11)

与往常一样,处理 const 关键字时,更改 C++ 语句中 const 关键字的位置具有完全不同的含义。上述 const 的用法仅适用于将 const 添加到函数声明末尾括号后的情况。

const 是 C++ 中过度使用的限定符:与指针结合使用时,语法和排序通常并不简单。有关 const 正确性和 const 关键字的一些读物:

Const 正确性

C++ 'const' 声明:为什么 &怎样做

A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a data member of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.

Another way of thinking about such "const function" is by viewing a class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of data members are possible.

It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)

As usual when dealing with the const keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const only applies when adding const to the end of the function declaration after the parenthesis.

const is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const correctness and the const keyword:

Const correctness

The C++ 'const' Declaration: Why & How

颜漓半夏 2024-09-14 11:03:24

考虑两个类类型变量:

class Boo { ... };

Boo b0;       // mutable object
const Boo b1; // non-mutable object

现在您可以在 b0 上调用 Boo任何成员函数,但只能调用 constb1 上的 code> 限定成员函数。

Consider two class-typed variables:

class Boo { ... };

Boo b0;       // mutable object
const Boo b1; // non-mutable object

Now you are able to call any member function of Boo on b0, but only const-qualified member functions on b1.

水波映月 2024-09-14 11:03:24

Bar 保证不会更改正在调用它的对象。例如,请参阅 C++ 常见问题解答中的有关 const 正确性的部分

Bar is guaranteed not to change the object it is being invoked on. See the section about const correctness in the C++ FAQ, for example.

‖放下 2024-09-14 11:03:24

类似于这个问题。

本质上,这意味着方法 Bar 不会修改非 Foo 的可变成员变量。

Similar to this question.

In essence it means that the method Bar will not modify non mutable member variables of Foo.

深海里的那抹蓝 2024-09-14 11:03:24

我总是发现从概念上更容易想到你正在将 this 指针设置为常量(这几乎就是它的作用)。

I always find it conceptually easier to think of that you are making the this pointer const (which is pretty much what it does).

梦回梦里 2024-09-14 11:03:24

函数无法通过您提供的指针/引用更改其参数。

每次我需要考虑它时,我都会转到此页面:

http:// /www.parashift.com/c++-faq-lite/const- Correctness.html

我相信 Meyers 的“更有效的 C++”中也有一个很好的章节。

Function can't change its parameters via the pointer/reference you gave it.

I go to this page every time I need to think about it:

http://www.parashift.com/c++-faq-lite/const-correctness.html

I believe there's also a good chapter in Meyers' "More Effective C++".

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