
27 Apr
2006
27 Apr
'06
4:45 p.m.
Kobi Cohen-Arazi wrote:
Will not work ! Why adding the & hurts? Kobi.
Because you attempt to bind a non-const reference to a temporary object (the return value of get(). The compiler refuses to do that. The compiler doesn't catch another possibly problematic situation, though, and that's replace's return by reference. You're returning a reference to a function parameter, and that's generally not a good idea. On the other hand, the removeConst function does absolutely nothing in your program except create yet another copy of the string. get() already returns a copy. The function is const, but its return value is not a const reference. Sebastian Redl