Article View: comp.std.c++
Article #1919Re: operator[]: Should I follow the conventional style?!
From: kanze
Date: Thu, 07 Sep 1995 15:04
Date: Thu, 07 Sep 1995 15:04
67 lines
3099 bytes
3099 bytes
In article <MATT.95Sep6110625@physics2.Berkeley.EDU>
martelli@cadlab.cadlab.it (Alex Martelli) writes:
|> kanze@gabi-soft.fr (J. Kanze) writes (in comp.lang.c++):
|> >Wil Evers (wil@ittpub.nl) wrote:
|> ...
|> >|> array[index].doSomethingToIt(args);
|> ...
|> >|> Please note that at least some compilers - I'm not sure if this is
|> >|> standard-conforming - will accept the second syntax even when we're using
|> >|> the proxy approach. They will call doSomethingToIt() on the temporary
|> >|> returned by the array's get() member function and then discard it. In
|> >|> other words, the array element at index is not changed at all!
|> in the context of something like (I'm synthesizing from word-level
|> descriptions by Kanze and Evers):
|> class A {
|> ...
|> public:
|> int doSomethingToIt(int args);
|> };
|> class proxyA;
|> class arrayA {
|> ...
|> public;
|> A& get(int index);
|> void put(int index, const A&it);
|> proxyA operator[](int index);
|> };
|> class proxyA {
|> arrayA& thearray;
|> int theindex;
|> public:
|> proxyA(arrayA& inar, int inin): thearray(inar), theindex(inin) {}
|> operator A() { return thearray.get(theindex); }
|> void operator=(const A& it) { thearray.put(theindex, it); }
|> };
|> In this case, the behaviour Wil describes is not standard conforming,
|> I believe; conversions are NOT supposed to be applied to the implicit
|> "this" operator of any member function, here doSomethingToIt()!
Correct.
Depending on the case, you can either provide all necessary forwarding
functions (which can be painful if there are a lot, and is not even
possible for template classes) or overload operator->() in the proxy,
and make the user write ``a[ x ]->someFunction()'' instead of
``a[ x ].someFunction()''. (What you really want to do is overload
operator.(), but the powers that be have decided otherwise.
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<9509070855.AA06861@slsvhdt.lts.sel.alcatel.de>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<KANZE.95Aug23140518@slsvhdt.lts.sel.alcatel.de> <1995Aug25.112209.1490@ittpub> <41klg4$235@gabi.gabi-soft.fr> <MATT.95Sep6110625@physics2.Berkeley.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: kanze <kanze@lts.sel.alcatel.de> Newsgroups: comp.std.c++ Subject: Re: operator[]: Should I follow the conventional style?! Date: 7 Sep 1995 15:04:52 GMT Organization: unk Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <9509070855.AA06861@slsvhdt.lts.sel.alcatel.de> References: <KANZE.95Aug23140518@slsvhdt.lts.sel.alcatel.de> <1995Aug25.112209.1490@ittpub> <41klg4$235@gabi.gabi-soft.fr> <MATT.95Sep6110625@physics2.Berkeley.EDU> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text Content-Length: 2191 X-Lines: 63 Originator: clamage@taumet