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

不问归期 提交于 2019-12-03 04:55:12

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.

Because they're evil and want you to suffer.

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

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

I have seen this in productive code already.

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

And the usecase was simple: Binary operations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!