返回 bool 的函数即使返回 1 或 0 也能工作
function returning bool works even though it returns 1 or 0
bool CWaypoint::less(CWaypoint const &wp_right)
{
bool result;
CWaypoint temp1;
(calculateDistance(temp1) > temp1.calculateDistance(wp_right)) ?
result = 1 : result = 0;
return result;
}
为什么即使我不返回布尔类型也不会收到错误?
发生整数到布尔值的转换。从C++标准:
4.12 布尔转换 [conv.bool]
算术、无作用域枚举、指针或指向成员类型的指针的 prvalue 可以转换为布尔类型的 prvalue 值。零值、空指针值或空成员指针值将转换为 false;任何其他值都将转换为 true。
相关文章:
- 将返回值存储在函数指针数组的指针中是如何工作的?
- c++ 11 带有 decltype 的尾随返回类型无法按预期工作
- 虚函数如何工作,分配后新的返回类型会发生什么?
- C++:返回本地对象,但不能正常工作
- 为什么我的类工作正常,即使在返回垃圾值作为赋值运算符和空复制构造函数的返回之后
- 我正在尝试从 GMocked 类返回一个 rapidjson::值,但我似乎无法让它工作
- 找不到使保证返回值优化工作的方法
- 按值返回的函数无法按预期工作
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 参数传递和返回值如何在程序集级别的 x86 上的 C/C++ 中工作?
- 返回对局部变量的引用是否正常工作?
- Waitforsingleobject在尝试打开Notepad++时工作,但立即返回Firefox
- 返回对象如何与分配运算符一起工作
- 返回指针的语句 - 解释它是如何工作的以及为什么
- 更改C 的返回指针后可以删除工作
- 模板返回类型函数如何在C++中工作
- CPU寄存器中返回的用户定义类型的C 对象.实例方法如何工作
- 防止Boost :: Asio :: IO_Context在没有更多工作时返回
- 返回一个结构数组(部分工作)C++
- c++运算符=工作异常(函数内部输出正常,但返回后错误)