如何检测用户定义数组的读写访问权限? [英] How to detect read or write access to a user defined array?
问题描述
嗨
假设我想创建一个只读项目数组
我重载[]运算符。如何检测我是否在阅读环境中被称为
foo =数组[23];
或者一个写上下文
数组[23] = foo;
据我记忆,这在C ++中是不可能的。
>
谢谢
Hi
Suppose that I want to create an array of read only items
I overload the [ ] operator. How can I detect if I am being called
within a read context
foo = Array[23];
or within a write context
Array[23] = foo;
As far as I remember, this is not possible in C++.
Thanks
推荐答案
" jacob navia" < ja *** @ jacob.remcomp.frwrote in message
news:46 *********************** @ news .orange.fr ...
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46***********************@news.orange.fr...
嗨
假设我要创建一个只读项目数组
Hi
Suppose that I want to create an array of read only items
如果你想要一个只读数组,那你为什么不返回const引用呢?
Abdo Haji- Ali
程序员
In | Framez
If you want a read-only array, then why don''t you return a const reference?
Abdo Haji-Ali
Programmer
In|Framez
Abdo Haji-Ali写道:
Abdo Haji-Ali wrote:
" jacob navia" < ja *** @ jacob.remcomp.frwrote in message
news:46 *********************** @ news .orange.fr ...
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46***********************@news.orange.fr...
>嗨
假设我要创建一个只读项目数组
>Hi
Suppose that I want to create an array of read only items
如果你想要一个只读数组,那你为什么不返回const引用呢?
Abdo Haji-Ali < br $>
程序员
In | Framez
If you want a read-only array, then why don''t you return a const reference?
Abdo Haji-Ali
Programmer
In|Framez
显然我需要初始化数组。
我想要一个只读数组的时间。
上下文:
我们在comp.lang上讨论过.c关于这一点,我只想知道
如果C ++中有可能在重载运算符中检测[]
如果我们被要求读取或者写作。
谢谢
Obviously I need to initialize the array.
I want a read only array MOST of the time.
Context:
We had a discussion in comp.lang.c about this, and I just want to know
if it is possible in C++ to detect within the overloaded operator [ ]
if we are being called for a read or for a write.
Thanks
" jacob navia" < ja *** @ jacob.remcomp.frwrote in message
news:46 *********************** @ news .orange.fr ...
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46***********************@news.orange.fr...
我们在comp.lang.c中讨论了这个问题,我只是想知道
如果我们被调用读取或写入,如果可以在C ++中检测重载运算符[]
。
We had a discussion in comp.lang.c about this, and I just want to know
if it is possible in C++ to detect within the overloaded operator [ ]
if we are being called for a read or for a write.
那么,我认为你不能。基本上是阅读。操作并不总是
一读。请考虑以下事项:
int& foo = Array [0]; //假设Array是一个整数数组
在这种情况下,可以使用''foo''来读取:
int iValue = foo ;
或写作:
foo = iValue;
甚至没有引用原始用户数组。
Abdo Haji-Ali,
程序员
In | Framez
PS :我会让这个索引器只读,并为
一次性提供其他功能。写。
Well then, I don''t think you can. Basically a "read" operation is not always
a read. Consider the following:
int& foo = Array[0]; // Assuming that Array is an integer array
In that case one can use ''foo'' for "reading":
int iValue = foo;
Or for writing:
foo = iValue;
Without even refering to the original user array.
Abdo Haji-Ali,
Programmer
In|Framez
PS: I would make this indexer read only and provide other function for
"one-time" write.
这篇关于如何检测用户定义数组的读写访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!