Re: ambiguous overload
onkar wrote:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);
void display(){
cout<<"3-display()// i="<<i<<endl;
}
};
Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;
You shouldn't return a reference to a local variable!
What should I do to prevent this ??
Stick to one operator+. Why have two?
--
Ian Collins.