什么时候调用const运算符[],什么时候调用非常量operator[]?
我有两种非常不同的读取和写入行为。在读取的情况下,我想复制一个相当难以提取的数据结构的缓冲区。在写入时,我将不缓冲地写入结构。
到目前为止,我一直使用operator[]来进行访问,所以为了多态性,我想继续这样做。
所以我的问题是:当进行访问时,会调用哪个版本?我的想法是,读取时调用 const,写入时调用非 const。在这种情况下,实现起来很简单。否则,事情可能会更加棘手。
I have two very different behaviors for reads and writes. In the event of reads, I want to copy a buffer of a rather hard to extract data structure. On writes, I will just write unbuffered to the structure.
Up to now, I have been using operator[] to do access, so for the sake of polymorphism I'd like to continue doing so.
So my question is this: When a access is made, which version is called? My notion is that the const is called for reads, and the non-const for writes. In that case, this is a simple thing to implement. Otherwise, it may be more tricky.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要完成您想要的任务,您通常需要让
operator[]
返回代理,并重载operator=
和operator T
(其中>T
是该代理类型的原始类型)。然后,您可以使用operator T
来处理读取,并使用operator =
来处理写入。编辑:代理的基本思想非常简单:返回一个代替原始对象的对象实例。目前,这将具有非常简单的语义(只需在向量中的指定索引处读取和写入字符);在您的情况下,operator= 和(特别是)operator T 内部的逻辑显然会更复杂,但这对基本结构影响很小或没有影响。
To accomplish what you want, you generally need to have
operator[]
return a proxy, and overloadoperator=
andoperator T
(whereT
is the original type) for that proxy type. Then you can useoperator T
to handle reads, andoperator =
to handle writes.Edit: the basic idea of a proxy is pretty simple: you return an instance of an object that acts in place of the original object. For the moment, this is going to have really trivial semantics (just read and write a char at a specified index in a vector); in your case, the logic inside of the
operator=
and (especially)operator T
will apparently be more complex, but that has little or no effect on the basic structure.重载决策基于
this
参数,即 - 基于您调用operator[]
的对象的常量性或缺乏常量性。The overload resolution is based on the
this
parameter, that is - on the constness or lack of constness of the object You call theoperator[]
on.由于读取和写入之间的行为如此不同,您可能至少要考虑不要重载
operator[]
,而是单独进行read
和write
> 方法。要回答您的问题,它基于您正在操作的对象的常量性。它不知道您将如何处理运算符的结果,因此它只处理一件事,即您调用运算符的对象。如果对象是非常量,它将始终调用运算符的非常量(写入)版本。您可以通过将const
const_cast
添加到对象上来解决这个问题,但单独的方法似乎是更好的方法。Since the behavior between reads and writes is so different, you might want to at least consider NOT overloading
operator[]
but instead making separateread
andwrite
methods.To answer your question, it's based on the const-ness of the object upon which you're operating. It doesn't know what you're going to do with the result of the operator so it has only one thing to work with, the object on which you're calling the operator. If the object is non-const it will always call the non-const (write) version of your operator. You can get around this by
const_cast
ing const onto the object, but separate methods seems like a better approach.这取决于您“分配”给什么:
将调用函数的 const 版本
或
将调用非 const 版本。最后,一个简单的作业
没有真正的方法可以说一个是“读”,另一个是“写”。基本上 const 版本将返回数据视图,其中未反映更新。非 consts 版本将返回数据视图,其中可以反映更新,但没有语义可以在写入后提供刷新。
It depends on what you're "assigning" to:
will call the const version of the function
or
will call the non const version. And finally, a simple assignment
There's no real way to say that one's a "read" and the other's a "write". Basically the const version will return a view into your data where updates are not reflected. The non-consts version will return a view into your data where updates can be reflected but there's no semantics to provide a refresh after a write.