【发布时间】:2021-03-05 06:43:54
【问题描述】:
如果两者都是 nullptr,那么检查这种方式是否安全,或者有更好的方法? 谢谢。
#include <iostream>
using namespace std;
int main()
{
int *x;
int *y;
if (not (x or y))
{
std::cout<<"both are nullptr";
}
return 0;
}
【问题讨论】:
-
是的,没关系。我会把它写成
if(!x && !y),在我看来这更直接,更容易阅读。 -
x,y未初始化,因此您的测试导致 UB...