gpt4 book ai didi

c++ - 返回 bool 的函数即使返回 1 或 0 也能正常工作

转载 作者:行者123 更新时间:2023-11-30 00:49:51 24 4
gpt4 key购买 nike

bool CWaypoint::less(CWaypoint const &wp_right)
{
bool result;
CWaypoint temp1;

(calculateDistance(temp1) > temp1.calculateDistance(wp_right)) ?
result = 1 : result = 0;

return result;
}

为什么即使我没有返回 bool 类型也没有收到错误消息?

最佳答案

发生整数到 bool 值的转换。来自 C++ 标准:

4.12 Boolean conversions [conv.bool]

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to aprvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;any other value is converted to true.

关于c++ - 返回 bool 的函数即使返回 1 或 0 也能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900158/

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