Can anyone please explain the difference between:
const char& operator[] const
and
char& operator[]
in C++?
Is it true that the second one is duplicating the string? and why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They both return references to the internal member of the string.
The first method is defined as a const method (the last const) and as such promises not to change any members. To make sure you can;t change the internal member via the returned reference this is also const.
This allows you to read members from the string:
For the second version it say we return a reference to an internal member. Neither promise that the object will not be altered. So you can alter the string via the reference.
No. Because it does not.