#include <iostream> using namespace std; class three_d { int x, y, z; public: three_d(int a, int b, int c) {x=a; y=b, z=c; } three_d operator+(three_d op2); friend ostream &operator<<(ostream &stream, three_d &obj); operator int() {return x*y*z;} }; ostream &operator<< (ostream &stream, three_d &obj) { stream << obj.x << ", "; stream << obj.y << ", "; stream << obj.z << endl; return stream; } three_d three_d::operator+ (three_d op2) { x+=op2.x; y+=op2.y; z+=op2.z; return *this; } int main() { three_d a(1, 2, 3), b(2, 3, 4); cout << a << b; cout << b+100 << endl; //31 line cout << a+b << endl; // 32 system("pause"); return 0; }
In line 31, object b
cast to int
, because the value of int is to the right, but why does ghost in line 32 work after operator+
executed? After all, there are two objects on both sides, why then the conversion function is called or what is it called there, and even at the end?