Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!mcnc!rti!ndl!gilley From: gilley@ndl.com (Greg Gilley) Newsgroups: comp.lang.c++ Subject: distinguishing operator[] on left and right Message-ID: <1991Feb28.212419.20920@ndl.com> Date: 28 Feb 91 21:24:19 GMT Sender: gilley@ndl.com (Greg Gilley) Organization: Numerical Design Limited Lines: 58 Is there any way to distinguish when the operator[] is used as an lvalue as opposed to an rvalue? The problem is that I am attempting to do a delayed copy of contents with a reference count (a sort of copy-on-write). So what I have is a class of the form: class X { struct Xrep { float *data; int refcnt; int length; } *p; . . . }; and operator= looks like: void X::operator=(const X &x) { x.p->refcnt++; if (--p->refcnt == 0) { delete p->data; delete p; } p = x.p; } Which is all well and good. If I do other operators, they create new ones, etc. The problem arises with []. Suppose we have: X a(1); X b(1); a[0] = 1.0; b = a; b[0] = 10.0; What you would like to have is a[0] == 1.0 and b[0] == 10.0 (which means that b has to be "detached" from a before it changes). However, I can't find a way to distinguish when the result of [] is being used as an lvalue or rvalue. Any suggestions? Thanks, Greg -- ------------------------------------------------------- Greg Gilley gilley@ndl.COM [Numerical Design Limited] 919-929-2917 (voice)