Xref: utzoo comp.lang.c++:12137 comp.std.c++:717 Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!van-bc!ubc-cs!alberta!aunro!ersys!markt From: ersys!markt@nro.cs.athabascau.ca (Mark Tarrabain) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: distinguishing operator[] on left and right Message-ID: Date: 10 Mar 91 00:25:38 GMT References: <1991Mar6.235058.3641@osceola.cs.ucf.edu> Organization: Edmonton Remote Systems, Edmonton, AB, Canada Lines: 36 ssd@engr.ucf.edu (Steven S. Dick) writes: > What if I have a [] operator that does something unusual to extract the > data from the object... for instance, a bitfield... > > // interface parts only... > class packedbits > { > public: > packedbits(int size); > int operator[](int index); > void set(int index); > void clear(int index); > }; > > doit() > { > packetbits flags(100); > > if (flags[4]) // this works > .... > > flags.set(4); // works--but ugly > flags[4] = 1; // how can I make this work??? > } > > Steve > ssd@engr.ucf.edu The line reading: int operator[](int index); should be: int &operator[](int index); then it will work on the left or the right side of an =. >> Mark