gpt4 book ai didi

c++ - const 非引用参数

转载 作者:行者123 更新时间:2023-12-02 10:04:25 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Use of 'const' for function parameters

(31 个回答)


2年前关闭。




const 的以下用法有什么值(value)吗?

void Func(const bool state) {
std::cout << "The state is: " << state << std::endl;
}
const当然推荐引用,我肯定看到它的好处。但是,变得简单 const除了非引用参数。那有什么用吗?

最佳答案

这些函数声明

void Func(const bool state);


void Func(bool state);

是等效的并声明相同的一个功能。

您甚至可以在编译单元中包含这两个声明。

但是,如果使用限定符 const 声明参数,则在函数定义中它可能不会被更改。

注意函数的参数是它的局部变量。

关于c++ - const 非引用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60979193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com