Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!sun-barr!lll-winken!ncis.tis.llnl.gov!dog.ee.lbl.gov!pasteur!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: distinguishing operator[] on left and right Message-ID: <11582@pasteur.Berkeley.EDU> Date: 1 Mar 91 21:07:00 GMT References: <1991Feb28.212419.20920@ndl.com> Sender: news@pasteur.Berkeley.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 32 In article <1991Feb28.212419.20920@ndl.com>, gilley@ndl.com (Greg Gilley) writes: |> Is there any way to distinguish when the operator[] is used as an |> lvalue as opposed to an rvalue? Yes, something like this: class ElementRef { operator int (); operator=(int); }; class IntVector { ... ElementRef operator[] (int); }; The idea is that [] returns a special class called ElementRef, which has two operators defined: assignment operator and cast-to-int. If I say IntVector v; int i, j; ... v[3] = i; j = v[4]; in the first case, ElementRef::operator=(int) is used. In the second case, ElementRef::operator int() is used. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck