#include <iostream>
using namespace std;
class A
{
public:
A(int a)
{
length = a;
}
~A(){}
friend A operator +(A& var1, A& var2);
A& operator=(A &other);
int length;
};
A operator +(A& var1, A& var2)
{
return A(var1.length + var2.length);
}
A& A::operator=(A &other)
{
length = other.length;
return *this;
}
int main()
{
A a(1);
A b(2);
A c(3);
c = a; // work
c = a + b; // does not work
cout << c.length ;
return 0;
}
In main(), c = a is successfully compiled but "c = a + b" is not. However, in A& A::operator=(A &other), if I change (A &other) into (A other) then it works. Can anyone help me with this case?
Aucun commentaire:
Enregistrer un commentaire