Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!netcom!sjsumcs!horstman From: horstman@mathcs.sjsu.edu (Cay Horstmann) Newsgroups: comp.lang.c++ Subject: Re: Distinguishing lvalue/rvalue operator[] Message-ID: <1991Mar7.003202.16879@mathcs.sjsu.edu> Date: 7 Mar 91 00:32:02 GMT References: <1991Mar3.202134.17812@mathcs.sjsu.edu> <1991Mar4.054915.19566@mathcs.sjsu.edu> <1991Mar5.021423.22117@world.std.com> Organization: San Jose State University - Math/CS Dept. Lines: 29 In article <1991Mar5.021423.22117@world.std.com> wmm@world.std.com (William M Miller) writes: >horstman@mathcs.sjsu.edu (Cay Horstmann) writes: >> I was thinking more about Joe Buck's suggestion to make the distinction >> of lvalue/rvalue operator[] by returning an access class, and found another >> snag. Suppose you have a variable size integer array and have operator[] >> return an instance of class Access with functions >> Access::operator=(int) >> Access::operator int >> as suggested. Then the statement >> a[i]++ >> will not execute. > >Of course not. (You wouldn't want it to, anyway -- presumably whatever you >do in operator=() to handle lvalue access also needs to be done for ++, no?) >The "Access" class is really acting as an enhanced functionality surrogate >for the addressed member (enhanced functionality because it's supposedly >doing different things for lvalue and rvalue accesses). That means that >anything you want to do with the accessed array member, you must be able to >do with an object of class Access. If you want to use ++, you need an >Access::operator++(). If you want to do ".print()," as in your earlier >posting, you need an Access::print(). It's really very straightforward -- >you look at what you need to do with selected array elements and you put the >appropriate support in the corresponding Access class. > Not as straightforward as you say. I am writing a template. I have no way of knowing what methods X includes, so I cannot manually replicate them in the access class. Cay