Topic: operator[]: Should I follow the conventional style?!


Author: martelli@cadlab.cadlab.it (Alex Martelli)
Date: 1995/09/06
Raw View
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()!

YesNo...?


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120

---
[ 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. ]