0

I've been asked in a class I'm taking to overload [] and return both a const and a non const character:

char * data_;
char & operator [] (size_t index);
const char & operator [] (size_t index) const;

I have the same implementation for both, and it compiles, but I'm pretty sure I'm missing something here:

const char & Array::operator [] (size_t index) const
{
    return data_[index]; // todo: needs to be const?
}

How can I ensure the returned character is not modifiable?

JoeB
  • 2,743
  • 6
  • 38
  • 51
  • Please excuse the strange_ private naming and the fact it isn't using a template, I can't change the header :) – JoeB Jan 14 '12 at 18:30
  • That naming is not strange. It's quite common, in fact, it's what I use. – Benjamin Lindley Jan 14 '12 at 18:32
  • 2
    Your implementation is correct, the const on the return type ensures that returned value is const. – Alok Save Jan 14 '12 at 18:32
  • Please ignore my comment.Somehow I overlooked a detail.Your overload is returning a `const char &` which already discards the possibility of using the return value as an l-value. If the return value was `char &` then my comment would be applicable here,but in that case I doubt James's answer would have pointed that out too :) – Alok Save Jan 14 '12 at 18:58

1 Answers1

2

How can I ensure the returned character is not modifiable?

The const in the return type, const char&, ensures that the character is not modifiable via the returned reference.


Note, however, that it might be preferable to return a char instead (i.e., a char by value, not a reference to a const char). Returning a const reference is useful when you have a large object (because you can avoid copying lots of bytes in many cases) or in generic code (because you don't know the size of all of the types that might show up), but when returning something known to be small (things don't get any smaller than char), you can just return it by value.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I see. So in my case, the implementation is the same, but once this becomes a template class (surely to happen later in the semester) the implementation will change? – JoeB Jan 14 '12 at 18:36
  • In this case probably OP needs to return a reference for `[]` so that the returned value can be used as an `l-value` which will allow it to appear on left side of an assignment. – Alok Save Jan 14 '12 at 18:40
  • Care to stick that in an answer? I assume you mean I have to return a reference somehow? (based on comments here http://stackoverflow.com/questions/6111905/c-is-return-value-a-l-value) – JoeB Jan 14 '12 at 18:43
  • @Als are you sure? I don't see why he needs to return a reference (i.e. why plain `char` isn't good enough). And you can't assign to a `const&` anyways. – Seth Carnegie Jan 14 '12 at 18:52
  • @Als: a `const char&` cannot appear on the left side of an assignment either. :-) – James McNellis Jan 14 '12 at 18:52
  • @JamesMcNellis: Ah that's correct, l-value overload is not needed for a const version. – Alok Save Jan 14 '12 at 18:54