C++ = operator

Suppose that objects A, B, and C are instances of class Foo. How should you design an assignment operator so that the "A=B=C;" statement would be allowed by a compiler but "(A=B)=C;" would not be allowed by a compiler?

Answer:



class Foo
{
private:
int a;
public:
const Foo& operator=(const Foo& other)
{
this->a = other.a;
return *this;
}
};

int main()
{
Foo a,b,c;
a = b = c;
(a = b) = c;
}

No comments: