为什么有人想要超载& (address-of)运算符? [英] Why would anyone want to overload the & (address-of) operator?

查看:269
本文介绍了为什么有人想要超载& (address-of)运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么合法的理由存在超负荷的一元运算符& ?


我只是读了这个问题,我不禁想到:



为什么任何人都想重载& (address-of)运算符? >

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



有没有合法用例?

解决方案

如果你正在处理任何类型的包装器对象,你可能想要或需要透明地将包装器的访问转发到包含的对象。在这种情况下,您不能返回指向包装器的指针,但需要重载address-of运算符以返回指向包含对象的指针。


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?

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

Is there any legitimate use case?

解决方案

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.

这篇关于为什么有人想要超载& (address-of)运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆