私人朋友操作符的代理类< [英] proxy class for a private friend operator&lt;&lt;

查看:105
本文介绍了私人朋友操作符的代理类<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以昨天我问这个问题私人朋友运营商<<
如何使运算符<< 私有的类。我得到了一个非常好的答案,
它完全按照我想要的,但我有一些额外的问题。



首先,我不明白那么代理类不必是 private_printable 的朋友呢?我如何可以从运算符<< ?中访问 private_printable 的内部成员?

其次,在使用回答代码时,我写了:

  proxy()const {return * this; } 

首先,它似乎对我和我甚至编译,但是当我运行它, segfault
我意识到我没有为代理类定义任何构造函数,
private_printable 作为参数,因此允许转换。
那么,我的代码如何编译,甚至没有给我任何警告?



然后我做的第三件事是给代理一个构造函数它有一个引用
private_printable 作为其参数(我没有使它显式),而不是定义转换操作符。
现在一切顺利,除了可以再次调用运算符<<
private_printable 。但我不太确定,为什么会这样。
是因为ADL吗?我对它有一些模糊的理解,但我不是
确定其所有的细节。无论如何,代理类是私有的,所以为什么
ADL会有什么区别?

解决方案


  1. 运算符<< 可以访问 private_printable ,因为它是 private_printable 朋友。它不是代理朋友


  2. return * this 返回一个 private_printable 对象,必须隐式转换为 code>。如何将 private_printable 转换为代理?调用 private_printable :: operator proxy()。但这是我们已经在的功能! segfault是由无限递归的堆栈溢出引起的。


  3. 非 - 显式构造函数也是一个转换函数,服务于运算符代理的相同目的。 ADL是一种涉及,就像使用运算符代理,没有构造函数。解决方案当然是使构造函数显式


这个问题只发生在特定情况下,构造函数的唯一操作数是代理类。 代理类是私有成员只有在明确命名时才有意义。 ADL仍然可以找到一个带有代理参数的函数,然后间接引用对转换函数的访问,因为类类型的使用不受访问限制的影响。只有名称查找受影响。同样,如果你只有一个 typedef ,你总是可以使用私人成员类型。


So yesterday I asked this question private friend operator<< about how to make operator<< private for the class. I got a very good answer, which worked exactly as I would like, but I have some additional questions about it.

First, I don't understand how come that the proxy class does not have to be friend of private_printable ? How is it possible that I have access to internal members of private_printable from within operator<< ?

Second, while playing with the code from answer, I wrote this:

operator proxy () const { return *this; }

At first it seemed OK to me and it even compiled, but when I run it and got a segfault I realized that I haven't defined any constructor for the proxy class, that would take private_printable as argument and thus allow the conversion. So how is it possible that my code compiled, without even giving me any warnings ?

Then the third thing I did, was to give the proxy a constructor that had a reference to private_printable as its argument (I did not make it explicit) instead of defining conversion operator. Now all went fine except that it was again possible to call operator<< on private_printable. But I am not really sure, why this happened. Is it because of ADL ? I have some vague understanding about it, but I am not sure of all its details. Anyways, the proxy class is private, so why would ADL make any difference ?

解决方案

  1. The operator<< can access members of private_printable because it's a friend of private_printable. It's not a friend of proxy.

  2. return *this returns a private_printable object, which must be implicitly converted to proxy. How to convert private_printable to proxy? Call private_printable::operator proxy (). But that's the function we're already in! The segfault is caused by stack overflow from infinite recursion. For what it's worth, an endless loop would be another possible outcome.

  3. The non-explicit constructor is also a conversion function, serving the same purpose as operator proxy. ADL is sort-of involved, the same as if the operator proxy is used and no constructor. The solution of course is to make the constructor explicit. And the problem only occurs for that specific case, when the sole operand of the constructor is the proxied class.

The proxy class being a private member only matters when you explicitly name it. ADL can still find a function with a proxy argument, which then indirectly causes access to a conversion function, because use of a class type isn't affected by access qualification. Only name lookup is affected. Likewise, you can always use a private member type if you just have a typedef to it.

这篇关于私人朋友操作符的代理类<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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