c ++函数语法/原型 - 括号后的数据类型

its*_*ols 6 c++ function

我非常熟悉C/C++标准函数声明.我最近看到过这样的事情:

int myfunction(char parameter) const
Run Code Online (Sandbox Code Playgroud)

以上只是一个假设的例子,我甚至不知道它是否有意义.我指的是参数之后的部分.常数.这是什么?

一个更真实的例子:

wxGridCellCoordsArray GetSelectedCells() const
Run Code Online (Sandbox Code Playgroud)

这可以在这里找到 那么该文本const在行尾的确切做什么?

aar*_*man 7

const表示该函数不会更改任何数据成员,this除非它们被标记为可变.
只有一个成员函数可以标记为const,这意味着在函数内部不会更改任何成员.


Pau*_*ton 5

The const keyword, when shown after a function, guarantees the function caller that no member data variables will be altered. It also changes the function signature, which is something less known to some C++ programmers. You can actually overload a function in C++ by adding the const keyword after a function with the same name. For example:

void changeValue() const;
void changeValue();
Run Code Online (Sandbox Code Playgroud)

Both of the above functions are valid and an overload of each other. I often see some C++ API's and frameworks use this overload to avoid a plethora of compile errors when users call these functions within const and non-const functions. Whether this is good C++ software engineering is up for debate. I would imagine it is bad practice, but it is good to be aware that it changes the function signature.

For instance given this class,

// In header
class Node {

public:

Node();

void changeValue() const;

~Node();

private:

int value;

};
Run Code Online (Sandbox Code Playgroud)

// in .cpp

void Node::changeValue() const {
    this->value = 3; // This will error out because it is modifying member variables
}
Run Code Online (Sandbox Code Playgroud)

There is an exception to this rule. If you declare that a member data variable is mutable, then it can be altered regardless if the function is declared to be const. Using mutable is for the rare situation where an object is declared constant, but in practice has member data variables which need the option to change. One potential example of its use is caching a value that you may not want to repeat the original calculation. This is typically rare... But it is good to be aware of it. A good reference of software engineering decisions around Mutable is the concept of bitwise const vs conceptual const. With bitwise const, the programmer is informing the reader that when const is present, no bits for that const object shall change without a const_cast. With conceptual const, the idea is that the user of the class should not care whether the bits of the mutable variable should change as it does not impact the usage of the class from the user's perception. Here is a good article explaining the difference and the ups and downs of using Mutable - https://www.cprogramming.com/tutorial/const_correctness.html

例如给定这个类,

// In header
class Node {

public:

Node();

void changeValue() const;

~Node();

private:

mutable int value;

};
Run Code Online (Sandbox Code Playgroud)

// 在.cpp

void Node::changeValue() const {
    this->value = 3; // This will not error out because value is mutable
}
Run Code Online (Sandbox Code Playgroud)

  • @itsols:您可能想要使用 `mutable` 的一个例子是,如果您的对象有一个“上次访问时间”时间戳变量。即使有人使用 `const` 方法访问对象,您也希望能够更新该变量。 (2认同)