Search code examples
c++operator-overloadingoverloadingaddress-operator

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


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?


Solution

  • 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.