关于C++运算符重载的问题
最近学习C++ 的运算符重载,看课本上的代码写了后输出显示和课本上的不一样,而且值很奇怪
#include <iostream> using namespace std; //using std::cout; //using std::endl; class Complex{ public: double real; double image; public: Complex(double real = 0, double image = 0){ this->real = real; this->image = image; } void display(){ cout << "(" << real << "," << image << ")" << endl; } friend Complex operator + (Complex A, Complex B); //重载 + friend Complex operator - (Complex A, Complex B); //重载 - friend Complex operator - (Complex A); //重载取负 friend Complex operator ++ (Complex &A); //重载前置自增 friend Complex operator ++ (Complex &A, int); //重载后置自增 }; Complex operator + (Complex A, Complex B){ return (A.real + B.real, A.image + B.image); } Complex operator - (Complex A, Complex B){ return (A.real - B.real, A.image - B.image); } Complex operator - (Complex A){ return (-A.real, -A.image); } Complex operator ++ (Complex &A){ cout << "pre" << A.real << "," << A.image << endl; return (++A.real, A.image); } Complex operator ++ (Complex &A, int){ cout << "post" << A.real << "," << A.image << endl; return (A.real++, A.image); } int main(){ Complex A(100.0, 200.0), B(-10.0, 20.0), C; cout << "A=", A.display(); cout << "B=", B.display(); C = ++A; cout << "C = ++A", C.display(); cout << "A=", A.display(); C = A++; cout << "C = A++"; C.display(); cout << "A=" ; A.display(); return 0; }
这里很奇怪,C输出的real值变成了200, image值变成了0,
还有下面的警告,不知道该怎么解决
希望有大佬能帮我一下
#C/C++#