473,585 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Aug 26 '07 #1
11 2183
"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:46******** *************** @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
Aug 26 '07 #2
Abdo Haji-Ali wrote:
"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:46******** *************** @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
Aug 26 '07 #3
"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:46******** *************** @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.
Aug 26 '07 #4
On Aug 26, 10:09 pm, "Abdo Haji-Ali"
<ah...@inframez .net_use_com_in steadwrote:
"jacob navia" <ja...@jacob.re mcomp.frwrote in message

news:46******** *************** @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

Aug 26 '07 #5
On 2007-08-26 15:59, z.cHris wrote:
On Aug 26, 10:09 pm, "Abdo Haji-Ali"
<ah...@inframez .net_use_com_in steadwrote:
>"jacob navia" <ja...@jacob.re mcomp.frwrote in message

news:46******* *************** *@news.orange.f r...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
Aug 26 '07 #6
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.


Aug 26 '07 #7
On Aug 26, 3:33 pm, jacob navia <ja...@jacob.re mcomp.frwrote:
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.

Aug 26 '07 #8
In article <46************ ***********@new s.orange.fr>,
ja***@jacob.rem comp.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.
Aug 26 '07 #9
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.
Aug 26 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
20431
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey...
1
13526
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the one that's about to load. I have a page that reloads every 30 seconds in order to access live data. If a user clicks on a link just prior to the...
9
3429
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so I'm just asking for a few pointers (or the full solution if you have the time) for what I want to do. Basically, I want to write a javascript...
23
6495
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I know if Window.Open has been redefined? BACKGROUND:
24
41571
by: Bob Alston | last post by:
Anyone know a way to make all access to a linked table, in another Access MDB, read only? I really don't want all the hassle of implementing full access security. I can't do this at the server file system because in some front ends the user needs update access. I want to give users access to the data for reporting with their own...
6
5146
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different documents (txt, MS Office files, pdf, pictures,…) from inside my app. It must start the file with the adequate external program (Notepad, MS Office...
1
5371
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved completely/succsessfully on the client's computer before updating some metadata on the server (file downloaded date for instance) However, All examples i have tried,...
1
1507
by: mCharton | last post by:
I'm pretty new to programming for SQL. Using VB.NET (2005) how do I detect whether a user has been given write access to my database at designtime in a windows application? Is there something akin to my.user.isinrole("Server/database/db_datawriter")??? I just need to be able to distinguish between users with write access and those with just...
36
5361
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers are configured to use Notepad to open the file by default and if they are configured to use Notepad by default I want it to remain that way rather...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.