- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我知道很少有关于 const 正确性的问题,其中声明函数的声明及其定义不需要就值参数达成一致。这是因为值参数的常量只在函数内部很重要。这很好:
// header
int func(int i);
// cpp
int func(const int i) {
return i;
}
这样做真的是最佳做法吗?因为我从未见过有人这样做。我在其他地方看到过这个引用(不确定来源),这已经被讨论过:
"In fact, to the compiler, the function signature is the same whether you include this const in front of a value parameter or not."
"Avoid const pass-by-value parameters in function declarations. Still make the parameter const in the same function's definition if it won't be modified."
第二段说不要将 const 放在声明中。我认为这是因为值参数的常量作为接口(interface)定义的一部分是没有意义的。这是一个实现细节。
基于这个推荐,指针参数的指针值是否也推荐? (它对引用参数没有意义,因为您不能重新分配引用。)
// header
int func1(int* i);
int func2(int* i);
// cpp
int func1(int* i) {
int x = 0;
*i = 3; // compiles without error
i = &x; // compiles without error
return *i;
}
int func2(int* const i) {
int x = 0;
*i = 3; // compiles without error
i = &x; // compile error
return *i;
}
总结:制作值参数对于捕捉一些逻辑错误很有用。这是最佳实践吗?您是否走到了将 const 排除在头文件之外的极端? const 指针值是否同样有用?为什么或为什么不?
一些引用资料:
C++ const keyword - use liberally? Use of 'const' for function parameters
const 值参数何时有用的示例:
bool are_ints_equal(const int i, const int j) {
if (i = j) { // without the consts this would compile without error
return true;
} else {
return false;
}
// return i = j; // I know it can be shortened
}
最佳答案
我已经读过很多次了,在函数中设置值参数是一件坏事,因为它是不必要的。
但是,我发现它有时对我很有帮助,因为它可以检查我的实现没有做我不打算做的事情(如您问题末尾的示例)。
因此,虽然它可能不会为调用者增加值(value),但它有时会为我作为实现者增加一点值(value),并且不会从调用者那里拿走任何东西。所以我认为使用它并没有什么坏处。
例如,我可能正在实现一个 C 函数,它接受几个指向缓冲区的指针——一个指向开始的指针,一个指向结束的指针。我打算将数据放入缓冲区,但要确保不会超出末尾。所以在函数内部有代码会在我向它添加数据时增加一个指针。将指向缓冲区末尾的指针设置为 const
参数将确保我不会编写一个错误,该错误会意外增加结束边界指针而不是我真正应该增加的指针。
所以一个带有这样签名的 fillArray 函数:
size_t fillArray( data_t* pStart, data_t* const pEnd);
将防止我在我真正想要增加 pStart
时意外增加 pEnd
。这不是什么大问题,但我很确定每个用过 C 语言编程过一段时间的人都遇到过这样的错误。
关于c++ - 值参数的 const 正确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1724051/
我有一个 const 方法,我想在其中将类 B 的一个成员的属性设置为当前实例 A(通过指针进行反向引用) A 类: void A::foo () const { ... ... B
Cython 生成的代码是否总是与生成它的 Python 代码一样正确? 它可能会帮助其他读者解决 Cython 静态类型声明和其他 Cython 功能(如果有的话)的使用问题,尽管我只对通过将 Py
我有一个包含一些指针的结构。我希望这些值是不可修改的。但是简单地写 const infront 并不能使结构成员不可变 typedef struct{ int *x; int *y; }poi
我需要评估和比较QR Code生成库,并寻找逻辑参数来测量和比较输出。 Why do I need this? It seems that when I give two different QR g
错误:Invalid conversion from 'char**' to 'const char**' 类似的问题似乎没有相同的情况(即两个函数在同一结构上具有不同的 const 要求)。如果确实
我确定以前有人问过这个问题,但所有搜索结果都有 const& 问题。 我想确保下面的方法不会改变传递给它的 GuestNode,所以我想传递 const GuestNode& guest,但是 g++
我有一个配置文件,它在我的程序运行时开始时被读入、解析并放入结构中。 我遇到的问题是我希望这些结构保持不变,因为它们中的值在程序生命周期内不应更改。 目前我正在做以下事情: 配置.h #pragma
我必须生成泊松分布的数据。我的范围是 n = 1000 到 100K。其中n是数据元素的数量; k 从 1 到 n 变化。它说使用 lambda 作为 n/2 我从未进行过统计,也不知道如何在这里获得
“如果每个顶点都可以从其他每个顶点到达,则称有向图是强连通的”。 Coreman 中给出的算法如下:- STRONGLY-CONNECTED-COMPONENTS (G) 1. Call DFS(G
我有一些代码是在不考虑 const 正确性的情况下编写的。有什么情况可以改变这个 class X { public: X(X& rhs); // does not modify rhs
我正在尝试对真正为 const 的类进行 const 操作 - 它不会更改该类指向的数据。 例如: class Node{ public: int val; }; class V{ publi
我没想到这段代码可以编译: #include #include class A { public: inline int get() const { return
将类的const 正确性 扩展到其指定成员的正确方法是什么?在示例代码中,get 方法的常量版本是否会创建一个 std::shared_ptr,其引用计数器与内部成员 m_b 相同,还是它重新从 0
我试图更好地理解 const-correctness 是如何工作的,更具体地说,在处理其成员基于 containers 和 smart pointers 的类时。我想无论类成员如何,const-cor
我有一个自定义迭代器实现(它迭代数据库查询结果,但这与此无关)。它在概念上有两组函数:get 类型函数,它从当前项目(当前行)返回一个值,以及 setup 类型函数(在我的例子中是 binds),它在
我目前面临一个由一些高级程序员编写的 C++ 项目,其中包含大约 400 个文件和 200 个类。 代码精心设计,运行良好且稳定。 虽然我正在添加一些功能,但对我来说,注意 const 的正确性只是普
这个问题在这里已经有了答案: What is the type of string literals in C and C++? (4 个回答) 关闭9年前。 根据 C++ 标准,字符串字面量类型是
我在一个类中有几个容器,例如 vector 或 map ,其中包含shared_ptr 指向堆上的对象。 例如 template class MyExample { public: private:
我知道很少有关于 const 正确性的问题,其中声明函数的声明及其定义不需要就值参数达成一致。这是因为值参数的常量只在函数内部很重要。这很好: // header int func(int i); /
对于本质上是只读但具有可能需要修改的互斥锁/锁的方法是否为 const 是否有约定? 如果没有,如果这种方法是 const 会有什么缺点/不好的设计 谢谢 最佳答案 您可以使用关键字mutable标记
我是一名优秀的程序员,十分优秀!