C ++函数语法/原型-括号后的数据类型 [英] c++ function syntax/prototype - data type after brackets

查看:146
本文介绍了C ++函数语法/原型-括号后的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C/C ++标准函数声明非常熟悉.我最近看过这样的东西:

I am very familiar with C/C++ standard function declarations. I've recently seen something like this:

int myfunction(char parameter) const

以上仅是一个假设示例,我什至不知道这是否有意义.我指的是参数之后的部分. const.这是什么?

The above is only a hypothetical example and I don't even know if it makes sense. I'm referring to the part AFTER the parameter. The const. What is this?

一个更真实的例子:

wxGridCellCoordsArray GetSelectedCells() const

可以在此处找到 那么该文本const在行尾到底是做什么的?

This can be found here So what exactly is that text const doing at the end of the line?

推荐答案

当在函数后显示const关键字时,可保证函数调用者不会更改任何成员数据变量.它还会更改函数签名,这是某些C ++程序员所不知道的.您实际上可以通过在具有相同名称的函数之后添加const关键字来使C ++中的函数过载.例如:

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();

以上两个函数均有效,并且彼此过载.我经常看到一些C ++ API和框架使用此重载来避免当用户在const和非const函数中调用这些函数时出现过多的编译错误.这是否是好的C ++软件工程尚有待商debate.我想这是一种不好的做法,但是很高兴知道它会更改函数签名.

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.

例如,给定此类,

// In header
class Node {

public:

Node();

void changeValue() const;

~Node();

private:

int value;

};

//.cpp

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

此规则有一个例外.如果您声明成员数据变量是可变的,则无论该函数是否声明为const,都可以对其进行更改.对于少数情况,使用可变的是将对象声明为常量,但实际上它具有需要更改选项的成员数据变量.使用它的一个可能的例子是缓存一个您可能不想重复原始计算的值.通常这种情况很少见.但是要意识到这一点是很高兴的.关于Mutable的软件工程决策的一个很好的参考是逐位const与概念const的概念.使用逐位const时,程序员将通知读者,当存在const时,如果没有const_cast,则该const对象的任何位都不得更改.使用概念const的想法是,该类的用户不必关心可变变量的位是否应该更改,因为它不会从用户的感知上影响该类的使用.这是一篇很好的文章,解释了使用Mutable的区别和跌宕起伏- https://www .cprogramming.com/tutorial/const_correctness.html

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;

};

//.cpp

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

这篇关于C ++函数语法/原型-括号后的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆