存在什么合理的理由使一元运算符&过载? [英] What legitimate reasons exist to overload the unary operator&?
问题描述
好的,我一直受到启发去做一些头部冲击.似乎超载operator&
会导致不小的痛苦.
Okay, I've been inspired to do some head punching. Seems like overloading operator&
leads to not a small amount of pain.
存在哪些合理的超载情况?
What legitimate cases exist for overloading it?
(不能说我曾经做过....)
(Can't say I've ever done that....)
推荐答案
我似乎记得类似智能指针类的东西,它覆盖了operator&
,因为它想返回所包含指针的地址而不是智能指针的地址.指针对象.不记得我在哪里看到过它,或者当时它似乎不是个好主意.
I seem to remember something like a smart pointer class which overrode operator&
because it wanted to return the address of the contained pointer rather than the address of the smart pointer object. Can't remember where I saw it or whether it seemed like a good idea at the time.
Aha,记得:微软的 CComPtr .
Aha, remembered: Microsoft's CComPtr.
为了概括起见,在以下情况下,可能是有意义的:
To generalize, it might make sense under the following conditions:
- 您有一个伪装成其他物体的物体.
- 该对象可以获取指向其伪装对象的指针.
返回合法指针以外的任何东西都将违反最小惊讶原则.
Returning anything other than a legitimate pointer would violate the principle of least astonishment.
这篇关于存在什么合理的理由使一元运算符&过载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!