L
learning_C++
Hi,
I compiled some code. In the function friend ostream&
operator<<(ostream& os, const complex c);
I use the later argument complex c and complex& c. I can get the same
values and there is no error when I compile this code.
Please tell me the difference.
#include <iostream>
using namespace std;
class complex{
double re, im;
friend ostream& operator<<(ostream& os, const complex c);
public:
complex(double r, double i):re(r), im(i){}
complex operator+(complex& c){
return complex(re + c.re, im + c.im);
};
//complex operator*(complex);
};
ostream& operator<<(ostream& os, const complex c){
os <<"("<<c.re<<","<<c.im<<")";
return os;
}
int main(){
complex a =complex(1, 3.1);
complex b=complex(1.2, 2);
complex c=b;
a =b+c;
cout<<a<<endl;
return 0;
}
I compiled some code. In the function friend ostream&
operator<<(ostream& os, const complex c);
I use the later argument complex c and complex& c. I can get the same
values and there is no error when I compile this code.
Please tell me the difference.
#include <iostream>
using namespace std;
class complex{
double re, im;
friend ostream& operator<<(ostream& os, const complex c);
public:
complex(double r, double i):re(r), im(i){}
complex operator+(complex& c){
return complex(re + c.re, im + c.im);
};
//complex operator*(complex);
};
ostream& operator<<(ostream& os, const complex c){
os <<"("<<c.re<<","<<c.im<<")";
return os;
}
int main(){
complex a =complex(1, 3.1);
complex b=complex(1.2, 2);
complex c=b;
a =b+c;
cout<<a<<endl;
return 0;
}