c++ - 通过 const 引用传递指针
问题描述
通过 const 引用传递指针不是为了优化吗?
前任。
bool testHelper(const TreeNode*& p, const TreeNode*& q) {
return false;
}
TreeNode* test(TreeNode* root, TreeNode* p, TreeNode* q) {
recursiveHelper(p, q);
}
错误:
Line 17: Char 28: error: binding reference of type 'const TreeNode *' to value of type 'TreeNode *' not permitted due to incompatible qualifiers
testHelper(p, q);
^
Line 12: Char 42: note: passing argument to parameter 'p' here
bool testHelper(const TreeNode*& p, const TreeNode*& q) {
^
解决方案
通过 const 引用传递指针不是为了优化吗?
不,不是,因为它并没有更快。添加的间接性可能会更慢。
bool testHelper(const TreeNode*& p, const TreeNode*& q)
这些不是对 const 的引用。这些是对指向 const 的非 const 指针的引用。
其他错误:
test
被声明为非无效返回但缺少返回语句- 您尚未声明
recursiveHelper
您调用的函数。
推荐阅读
- excel - 有没有办法通过 VBA 查找和选择第二个变量范围?
- http - 为什么我们需要 HTTP 而不是 SSH?
- python - 希望从字典中的列表中获取特定值
- java - “==”运算符何时成功比较字符串?
- go - 如何从 grpc-server 流式传输结果以及如何从 goroutines grpc server-stream 返回结果以将结果传递给 api
- webpack - 在 webpack 中使用 splitChunks 选项时如何修复模块未定义错误
- swift - 如何将字符串转换为 UIColor (Swift)
- javascript - 如何更改我搜索的文本颜色?
- android - ADB 命令显示删除帐户屏幕
- python - 尝试在 python 中使用 hyperopt 调整最近邻居时出错