我在一行中使用流操作符< <和位移操作符< <。 我有点困惑,为什么代码A)不会产生与代码B相同的输出)?operator <<:std :: cout << i <<(i << 1);
A)
int i = 4;
std::cout << i << " " << (i << 1) << std::endl; //4 8
B)
myint m = 4;
std::cout << m << " " << (m << 1) << std::endl; //8 8
类敏:
class myint {
int i;
public:
myint(int ii) {
i = ii;
}
inline myint operator <<(int n){
i = i << n;
return *this;
}
inline operator int(){
return i;
}
};
在此先感谢
哎呀
几乎重复的:http://stackoverflow.com/questions/2603312/the-result-of-int-c0- coutcc /。对于大多数实际用途来说,它们是相同的,尽管使用“++”而不是“<<”作为修改操作符。 – 2010-04-21 21:19:35
@Jerry Coffin:它们非常相似,但有整个“vs < <混淆和事实上<<通常不会改变它的论点 – 2010-04-21 21:23:50