初学orz用成员函数重载前增操作符怎么返回值?运行结果很奇怪。。
#include<iostream>using namespace std;
class xs
{
private:
double re,im;
public:
xs(double r,double i);
void print()const;
xs operator*(const xs& b);
xs& operator++();
xs operator++(int);
};
xs:: xs(double r,double i)
{
re=r;
im=i;
}
void xs::print()const
{
cout<<re<<"+"<<im<<"i"<<endl;
}
xs xs::operator*(const xs& b)
{
xs t(re*b.re-im*b.im,re*b.im+im*b.re);
return t;
}
xs& xs::operator++()
{
++re;
++im;
xs a(re,im);
return a;
}//这个地方运行结果出问题了
xs xs::operator++(int)
{
xs ori(re,im);
++re;
++im;
return ori;
}
int main()
{ xs z1(1,2),z2(3,4);
(z1*z2).print();
(z1++).print();
z1.print();
(++z1).print();
system("pause");
return 0;
}
运行结果:
-5+10i
1+2i
2+3i
-9.25596e+061+-9.25596e+061i//这个结果。。。