#include <iostream>
using namespace std;
struct X{
int _x;
X(int x=0):_x(x){}
};
int main() {
X arr[5];
for (int i = 0;i < 5;i++) {
arr[i] = i;
}
for (int i = 0;i < 5;i++) {
cout << arr[i] <<",";
}
return 0;
}
When must I overload operator <<
?
I think since arr
has ints so I shouldn't overload <<
?
Although your X
is just a wrapper around a singular int
property it is a different type than int
. The good news is you can just delegate to the int
method by adding this function:
std::ostream& operator<<(std::ostream& o, const X& x) {
o << x._x;
return o;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.