Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to detect read or write access to a user defined array? (http://www.velocityreviews.com/forums/t532810-how-to-detect-read-or-write-access-to-a-user-defined-array.html)

jacob navia 08-26-2007 12:33 PM

How to detect read or write access to a user defined array?
 
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 08-26-2007 12:54 PM

Re: How to detect read or write access to a user defined array?
 
Abdo Haji-Ali wrote:
> "jacob navia" <jacob@jacob.remcomp.fr> wrote in message
> news:46d17343$0$27399$ba4acef3@news.orange.fr...
>> Hi
>>
>> Suppose that I want to create an array of read only items

> If you want a read-only array, then why don't you return a const reference?
>
> Abdo Haji-Ali
> Programmer
> In|Framez
>
>


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

Abdo Haji-Ali 08-26-2007 01:46 PM

Re: How to detect read or write access to a user defined array?
 
"jacob navia" <jacob@jacob.remcomp.fr> wrote in message
news:46d17343$0$27399$ba4acef3@news.orange.fr...
> Hi
>
> Suppose that I want to create an array of read only items

If you want a read-only array, then why don't you return a const reference?

Abdo Haji-Ali
Programmer
In|Framez



z.cHris 08-26-2007 01:59 PM

Re: How to detect read or write access to a user defined array?
 
On Aug 26, 10:09 pm, "Abdo Haji-Ali"
<ah...@inframez.net_use_com_instead> wrote:
> "jacob navia" <ja...@jacob.remcomp.fr> wrote in message
>
> news:46d1781d$0$25947$ba4acef3@news.orange.fr...> 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.

>
> 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.


And also I could use *(Array + index) = value, to change the array
whatever you do to [].
So, if you want to achieve your goal, just overload the [] operator is
not enough.

--
Regards
Chris D. Cheng


Abdo Haji-Ali 08-26-2007 02:09 PM

Re: How to detect read or write access to a user defined array?
 
"jacob navia" <jacob@jacob.remcomp.fr> wrote in message
news:46d1781d$0$25947$ba4acef3@news.orange.fr...
> 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.

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.



=?ISO-8859-1?Q?Erik_Wikstr=F6m?= 08-26-2007 02:36 PM

Re: How to detect read or write access to a user defined array?
 
On 2007-08-26 15:59, z.cHris wrote:
> On Aug 26, 10:09 pm, "Abdo Haji-Ali"
> <ah...@inframez.net_use_com_instead> wrote:
>> "jacob navia" <ja...@jacob.remcomp.fr> wrote in message
>>
>> news:46d1781d$0$25947$ba4acef3@news.orange.fr...> 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.

>>
>> 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.

>
> And also I could use *(Array + index) = value, to change the array
> whatever you do to [].
> So, if you want to achieve your goal, just overload the [] operator is
> not enough.


Assuming that the items are stored in an array, that is.

--
Erik Wikström

Rolf Magnus 08-26-2007 04:32 PM

Re: How to detect read or write access to a user defined array?
 
jacob navia wrote:

>>> Suppose that I want to create an array of read only items

>> If you want a read-only array, then why don't you return a const
>> reference?

>
> Obviously I need to initialize the array.


Then use the object through a const reference after initializing.

> 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.


It is not called "for a read or for a write". MyObject[i] just calls the
operator[] on MyObject. What you do to the object returned by that
operator is a whole different story. So the answer to your question would
be "no". However, you could let the operator create a proxy object that
forwards read and write operations, which might be sufficient, depending on
what you want.





terminator 08-26-2007 04:36 PM

Re: How to detect read or write access to a user defined array?
 
On Aug 26, 3:33 pm, jacob navia <ja...@jacob.remcomp.fr> wrote:
> 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


something like this will do for lots of cases:

template <class T>
struct contianer{
T& operator[](unsigned);
const T& operator[](unsigned) const;
};

but if you need a class for which iteration for read and write need
different algorithms, you can easily define an indexer class:

template <class T>
struct contianer{
struct indexer{
explicit indexer(T*const,unsigned i):me(T),index(i){};
operator T&()const;//this is the read function
indexer& operator=(const T&)const{/*the 'write' stuff
here*/};
protected:
T* const me;
const unsigned index;
private:
indexer(const indexer&);
};
indexer operator[](unsigned i){return indexer(this,i);};
const T& operator[](unsigned) const{return indexer(this,i).
(operator T&)();};
...//class definition continues
};

regards,
FM.


Jerry Coffin 08-26-2007 04:47 PM

Re: How to detect read or write access to a user defined array?
 
In article <46d17343$0$27399$ba4acef3@news.orange.fr>,
jacob@jacob.remcomp.fr says...
> 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;


Make it const and have it return a reference to const. This prevents you
from writing to the data (at all) via the operator:

class bad_subscript {};

template <class T>
class const_array {
T *data_;
size_t size_;
public:
const_array(T *init, size_t size) :
size_(size), data_(new T[size])
{
std::copy(init, init+size, data_);
}

T const &operator[](size_t subscript) const {
if (subscript > size_)
throw bad_subscript();
return data_[subscript];
}

T *raw_data() { return data_; }
size_t size() { return size_; }
};

raw_data() is a quick hack to allow writing to the data -- you haven't
said when or how you want to allow writing to the data, so I've provided
one way to do it. If you _only_ want to allow the data to be
initialized, you can eliminate it (and probably add more ctors to allow
more than one form of intialization).

--
Later,
Jerry.

The universe is a figment of its own imagination.

terminator 08-26-2007 04:57 PM

Re: How to detect read or write access to a user defined array?
 

> but if you need a class for which iteration for read and write need
> different algorithms, you can easily define an indexer class:
>
> template <class T>
> struct contianer{
> struct indexer{
> explicit indexer(T*const,unsigned i):me(T),index(i){};

explicit keyword is extra
> ......
> private:
> indexer(const indexer&);


oops!! delete last two lines(they were error)
>

regards,
FM.




All times are GMT. The time now is 09:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.