const_cast
and Overloading
In § 4.11.3 (p. 163) we noted that const_cast
s are most useful in the context of overloaded functions. As one example, recall our shorterString
function from § 6.3.2 (p. 224):
// return a reference to the shorter of two stringsconst string &shorterString(const string &s1, const string &s2){ return s1.size() <= s2.size() ? s1 : s2;}
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.