Topic: Need an operator ->[]


Author: Sean A Corfield <sean@corf.demon.co.uk>
Date: 1995/10/18
Raw View
In article <461ref$hfr@sake.wwa.com>,
Shashaanka <shashank@sashimi.wwa.com> wrote:

|> X x;
|> X * x1=x;

Firstly, I'll assume you mean:

 X* x1 = &x;

|> However, a more natural way could be to use it like
|> x1->[10].
|>
|> Would like your comments on this, or if there are alternatives.

I'm not sure I like your proposed syntax but why not use a reference
instead?

 X& x2 = x;
 x2[10];

[shared memory justification snipped]
You could still use references even when pointers would seem natural due to
allocation:

 X& x3 = *new X[10];
 x3[10];   // invoke X::operator[], return int
 (&x3)[9]; // invoke builtin [] on X*, return X&
 delete[] &x3;
---
[ 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. ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1995/10/18
Raw View
>>>>> "S" == Shashaanka  <shashank@sashimi.wwa.com> writes:
[...]
S> X x;
S> X * x1=x;

Are you sure this is correct? Did you intend to write `X* x1 = &x;'
instead?

[...]
S> (*x1)[10] to access 10th element.
S> However, a more natural way could be to use it like
x1-> [10].

S> Would like your comments on this, or if there are alternatives.
[...]

Are you aware of ``references''? E.g., for the code above:
 X& x1 = x;
 // ... x1[10] ... This syntax now available.

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