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

Accepted answer
Score: 33

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

Score: 8

Because they're evil and want you to suffer.

Or 6 I guess if you are using proxy objects? I 5 suppose you might want to return a pointer to the 4 managed object instead of the container 3 - although i'd rather do that with a getter 2 function. Otherwise you'd have to remember 1 to use things like boost::addressof.

Score: 8

Yes, for debugging (if you want to trace 2 any access or reference, you might want 1 to put a log line on any call to &, * or ->).

Score: 0

I have seen this in productive code already.

But 3 there, a binary representation of the content 2 of a struct was returned, not just 0.

And the usecase 1 was simple: Binary operations.

More Related questions