0

http://cplusplus.com/reference/string/basic_string/operator[]

I understand that it's advantageous to have a second version which returns const to prevent warnings when a const result is required and to mitigate casting but if the function already provides a non-const method (method-- not result) then what is the point of declaring the const-result method const?

Tas
  • 7,023
  • 3
  • 36
  • 51
Zhro
  • 2,546
  • 2
  • 29
  • 39

5 Answers5

4

You need to understand, that the second (const) version not only returns a different result but is also marked itself as const (this is the second const at the end of the declaration):

const_reference operator[] (size_type pos) const;

These are two different things: The const return value by itself would not be needed in the presence of a non-const method (since the non-const return value can always be casted tò the const version).

But not having a const-version of the operator would mean, that you couldn't use it on const string objects.

The const result is just a result of the constness of the operator itself: if you have a const string and use the operator to get a reference to a single character, clearly this reference must also be const (if not, you could change single characters within a const string).

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
  • OH OF COURSE! It's referring to the object itself being const. This the allows filtering methods for the same type of object which is either `const` or non-`const`. This is the clue I was missing. Having been programming in Java for a while, I'd forgotten that this is how we deal with immutable types without having to create a duplicate "immutable" class of the same object. – Zhro Jan 25 '16 at 01:04
3

Assume you have

const std::string str{"test"};
std::cout << str[2];

If you don't have a const member function, the code above will fail, as the this pointer passed implicitly to operator[] is const.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
2

If you have a const std::basic_string you cannot call (non-const) std::basic_string::operator[], as it is, well, not marked const.

Diego Elio Pettenò
  • 3,152
  • 12
  • 15
1

If there wasn't a const version of the function, the function could not be invoked on a const instance. For example:

void func (const std::string &h)
{
    if (h[0] == "hello")
        ...
}

How would this work if there wasn't a const version of operator[]?

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 2
    I'm not the downvoter, but maybe the typo or maybe someone thought it was incomplete (`if (h[0] ==`)? – Tas Jan 25 '16 at 00:50
0

The const method returns a different type, it returns a const-qualified reference. This means if the const method is used, the result of the method is const-qualified (and therefore is “immutable”). The non-const-qualified method returns a regular reference, which is “mutable”.

You can't have just the const method or else you can't modify individual characters in non-const-qualified std::basic_string objects. And you can't have just the non-const method or you won't be able to call the method on const-qualifed std::basic_string objects.

dreamlax
  • 93,976
  • 29
  • 161
  • 209