[comp.lang.c++] detecting writes

sk@macbeth.cs.duke.edu (Srinivasan Krishnan) (07/25/90)

While trying to implement an array based object, I have run into 
difficulties with detection of writes on the array.

Here's part of the definition of the class:

	class IntArray {
	public :

			// allows assignments of the form A = B
		IntArray& operator=(const IntArray&);

			// returns the lvalue of the i-th element of the array
		int& operator[] (int);

	protected :
			// internal data representation
		int flag;
		int *ia;
};

I would like to be able to set the variable "flag" to zero whenever any
assignments are made to the array.

	Assignments to an array are of 2 types : 

1	A = B ; ( A and B IntArrays )
	By overloading the = operator , I am able to 
	set the flag of A to 0 

2       A[i] = somevalue;
	This is causing the problem. The [] operator is used for
	both reads and writes, and the = operator operates on integers.

Is there any way I can cause the side effect of setting flag to 0
only on memory writes while retaining the use of the [] operator?

thomasw@hpcupt1.HP.COM (Thomas Wang) (07/26/90)

Your problem is that the [] operator is defined as returning a reference.
Change it to return a normal object, then add a
void put_at(int, array_element_type&);

 -Thomas Wang
              (Everything is an object.)
                                                     wang@hpdmsjlm.cup.hp.com
                                                     thomasw@hpcupt1.cup.hp.com

roger@procase.UUCP (Roger H. Scott) (08/02/90)

I posted a detailed example of "virtual arrays" about two weeks ago.
Go back and look at the last few weeks' news.