Why do the STL containers define const and non-const versions of accessors ?
What is the advantage of defining const T& at(unsigned int i) const and T& at(unsigned int) and not only the non-const version ?
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.
Because you wouldn’t be able to call
aton aconstvector object.If you only had the non-
constversion, the following:would not compile. Having the
constversion makes this possible, and at the same time prevents you from actually changing whatatreturns – which is by contract, since the vector isconst.The non-
constversion can be called on a non-constobject and allows you to modify the returned element, which is also valid because the vector isn’t const.