在有效的c++中使用赋值
Is assigning a value while using it valid c++?
我有一个布尔值,每次使用它都需要翻转,因为代码相当简单,每隔一行我就翻转布尔值。我摆弄了一下,想出了这个(甚至更简单的例子)
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
bool flippy = 0;
cout << (flippy = !flippy) << "n";
cout << (flippy = !flippy) << "n";
cout << (flippy = !flippy) << "n";
cout << (flippy = !flippy) << "n";
system("PAUSE");
return 0;
}
它像预期的那样产生1 0 1 0,但看起来有点奇怪,这是语言的有效使用吗?
是有效的。这不是我所说的好风格
您的代码当然有效。下面以bitset
为例进行说明。
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<1> myBit;
cout << myBit << endl;
cout << myBit.flip() << endl;
cout << myBit.flip() << endl;
cout << myBit.flip() << endl;
}
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 用C++中的sscanf赋值
- 为std::string的某个索引赋值
- 重载Singly Linked List中的赋值运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- gtest_使用setargpointee在函数中赋值
- 非常量变量只读位置的赋值
- 使用赋值运算符重载从类中返回jobject
- C++数据文件、数组和计算赋值
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 标准库类型的赋值运算符的引用限定符
- 关于 c++ 函数中指针赋值的简单问题
- 复制构造函数、赋值运算符C++
- 标准::变体的赋值运算符
- cin >> int 给定一个字符串将 int 赋值为 0
- if 子句中的赋值不起作用
- 复制包含C++所有元素的对象!(构造函数和赋值,最佳实践?