Why would anyone want to overload the & (address-of) operator?

Tony the Pony picture Tony the Pony · Jun 28, 2011 · Viewed 10.1k times · Source

Possible Duplicate:
What legitimate reasons exist to overload the unary operator& ?

I just read this question, and I can't help but wonder:

Why would anyone possibly want to overload the & ("address-of") operator?

SomeClass* operator&() const {
    return address_of_object;
}

Is there any legitimate use case?

Answer

Timo Geusch picture Timo Geusch · Jun 28, 2011

If you're dealing with any sort of wrapper objects, you might want or need to transparently forward the access to the wrapper to the contained object. In that case, you can't return a pointer to the wrapper, but need to overload the address-of operator to return a pointer to the contained object.