琐碎的话题,作业是否返回"假"?
Trivial topic, does an assignment return 'false'?
在这里,结果是6。但是i=5不是一个非零值吗?如果我输入I +=5,它就会认为它为真。这有什么不同?(也不,我不是故意把I ==5)
int i=7;
if(i=5) {
cout << ++i;
} else {
cout << --i;
}
像=
和+=
这样的赋值操作符返回对象被赋值后的值。所以,如果你赋值false
或0
,你可以从赋值操作符中得到false
。
i=5
计算为5
,即if ()
眼中的true
。但是i=0
会被计算为0
,而if ()
会被认为是false
。
赋值返回所赋的值。在您的示例中:
int i = 7;
if (i = 5) { // returns 5, which is non-zero, or "true"
cout << ++i; // prints 6, or 5+1
} else {
cout << --i; // would print 4, or 5-1, if it was hit, which it never will
}
您可能会对前递增和后递增感到困惑。例如,考虑以下内容:
int i = 7;
if (i = 5) { // returns 5, which is non-zero, or "true"
cout << i++; // prints 5, i is 6 after this line
} else {
cout << i--; // would print 5, but i is 4 after this line
}
你的代码是这样的:
i = 7;
i = 5;
if ( 5 ) // it's true. Isn't it ?
{
i = i + 1; // now i is 6
cout << i;
}
相关文章:
- 函数作为模板参数,是否对返回类型强制约束
- lock_guard是否保护返回值
- 检查函数返回类型是否与STL容器类型值相同
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 返回指向对象的指针的函数调用是否为 prvalue?
- 是否所有C++运算符都返回一些内容
- 如果返回 -1,时间() 的参数是否被修改?
- 是否可以在 QAbstractItemModel 中返回正数行数和零列数?
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 插入向量时,使用lambda的返回而不是函数的返回是否有意义?
- 默认情况下,返回是否使用移动或复制语义
- 在所有std::futures完成之前从函数返回是否安全
- 将成员函数指针强制转换为另一个类中的一个然后再返回是否安全
- 临时对象的成员函数的左值引用返回是否为悬空引用
- 通过引用成员变量EVER返回是否可接受
- 本例中引用静态变量的引用返回是否安全?
- 从 typeid 成功返回是否保证dynamic_cast不会引发异常?
- new 运算符 - 在C++中测试"new"的返回是否有用?