然后在 C++ 条件中
And then in the C++ conditional
除了 Boolean and
operator 之外,Ada 还具有在 if
条件中使用的 and then
语句的良好特性。这允许在访问对象之前检查对象是否不为 null,如下所示:
if Object /= null and then Object.Value > 5 then
-- do something with the value
end if;
有没有一种方法可以使用嵌套 if
在 C++ w/o 中表达类似的行为?
嗯,Object
在您的代码中的 C++ 中不能为 NULL,因为它似乎不是指针。如果它是一个指针,你可以说:
if (Object && Object->Value > 5 ) {
// do something
}
在 C++ 中,&&
运算符执行 "short-circuited evaluation" - 如果 left-most 操作数的计算结果为假,则计算停止。
除了 Boolean and
operator 之外,Ada 还具有在 if
条件中使用的 and then
语句的良好特性。这允许在访问对象之前检查对象是否不为 null,如下所示:
if Object /= null and then Object.Value > 5 then
-- do something with the value
end if;
有没有一种方法可以使用嵌套 if
在 C++ w/o 中表达类似的行为?
嗯,Object
在您的代码中的 C++ 中不能为 NULL,因为它似乎不是指针。如果它是一个指针,你可以说:
if (Object && Object->Value > 5 ) {
// do something
}
在 C++ 中,&&
运算符执行 "short-circuited evaluation" - 如果 left-most 操作数的计算结果为假,则计算停止。