# const whose const is this?

## Recommended Posts

draconar    130
I left this piece of code alone so I would think about it at a later time. So this later time came, it is now. :D
class Str{
public:
...
const char& operator[](size_type i) const {return data[i];}
};


Please correct me if I'm wrong, but the first "const char&" will be the return type I'm agreeing to, well, return. but what about the other const keyword right at the end of the operator overload definition? If I'm correct, it is telling the compiler that size_type will be a const iterator, but what about it? should the last const be INSIDE the parameters list? something like (const size_type i){}? hear from you!

Sneftel    1788

##### Share on other sites
Zahlman    1682
Quote:
 Original post by draconarwhat about the other const keyword right at the end of the operator overload definition?

It promises that calling the operator overload (naturally, this works the same way with ordinary member functions) will not change the called-upon Str instance. Thus you are allowed to index into a const Str. This works because you are returning a const reference to the indexed char, so the caller can't use it to change the object.

If you want the calling code to be able to do things like "my_str[42] = 'c'", then you'll need a non-const version that returns a non-const reference to the char. The calling code will, of course, assign to the Str's data via the reference returned by the operator[].

Normally you should provide both. (This is from just a little further down the page that Sneftel linked.)

##### Share on other sites
Storyyeller    215
Well a const function can change things, although it's usually a bad idea. If you have internal variables for caching or something, you can make them mutable so that a const function can modify them.

##### Share on other sites
draconar    130
Quote:
 Original post by ZahlmanIf you want the calling code to be able to do things like "my_str[42] = 'c'", then you'll need a non-const version that returns a non-const reference to the char. The calling code will, of course, assign to the Str's data via the reference returned by the operator[].

How would the const const version be called by the code? I should have a const reference first in order to assign it?

##### Share on other sites
haegarr    7372
Quote:
 Original post by draconarHow would the const const version be called by the code? I should have a const reference first in order to assign it?

AFAIK (i.e. based on experience rather than on specifications) if you have any of these const versions
const char& get() const;char const& get() const;char get() const;
and this non-const version
char& get();
and you use it for the purpose of reading like
char myChar = get();
then the compiler will use the const version. If, on the other hand, you use it for the purpose of writing like
get() = myChar;
then the compiler will use the non-const version. Of course, assigning get()'s result to a const pointer will work, too.

##### Share on other sites
SiCrane    11839
That would be incorrect. Which version is called depends on what type the member function is being called on. If it's a const pointer or reference to the object the const member function is called. If it's a non-const pointer or reference then the non-const member function is called.

##### Share on other sites
haegarr    7372
Quote:
 Original post by SiCraneThat would be incorrect. Which version is called depends on what type the member function is being called on. If it's a const pointer or reference to the object the const member function is called. If it's a non-const pointer or reference then the non-const member function is called.
Okay, then my observations have misleaded me.