Create a non-friend inserter.
#include <iostream>
using namespace std;
class MyClass {
public:
int x, y; // must be public
MyClass() {
x = 0;
y = 0;
}
MyClass(int i, int j) {
x = i;
y = j;
}
};
// An inserter for the MyClass class.
ostream &operator<<(ostream &stream, MyClass ob)
{
stream << ob.x << ", " << ob.y << '\n';
return stream;
}
int main()
{
MyClass a(1, 1), b(10, 23);
cout << a << b;
return 0;
}
Related examples in the same category