Page 1 of 1

const? (more C++)

PostPosted: Sun Oct 01, 2006 10:18 pm
by Slater
ok, so in my code I have a piece of line that looks like... this...
const cLinkedStrings operator +(const cLinkedStrings& CLS2) const;


ok, so what it does is overloads the + operator for adding objects of type cLinkedStrings. No sweat there, however the prototype has me wondering... what exactly is that last "const" there for? Nobody explained it in class, and the books I have don't explain it either, they just do it. Maybe I'm just missing out on something obvious here?

PostPosted: Tue Nov 14, 2006 2:47 am
by Warrior4Christ
GAVEDIG'D!!

I finally worked it out for myself... but it seems to not apply in this case, unless you typed it incorrectly.

It would make sense if it was:
const cLinkedStrings &operator+(const cLinkedStrings &CLS2) const;
because then it returns a reference to the returned value (so it doesn't need to bother wasting time and space creating a copy), but if it wasn't constant, then you would be able to change the original value. And you don't want that. So make the result a constant reference.

But I don't know if you wanted a constant reference return value...