const? (more C++)

Homework giving you a headache? Math gives you a migraine? Can't quite figure out how to do something in photoshop? Never fear, the other members of CAA share their expertise in this forum.

const? (more C++)

Postby Slater » Sun Oct 01, 2006 10:18 pm

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?
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Warrior4Christ » Tue Nov 14, 2006 2:47 am

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...
Everywhere like such as, and MOES.

"Expect great things from God; attempt great things for God." - William Carey
User avatar
Warrior4Christ
 
Posts: 2045
Joined: Sat Aug 20, 2005 8:10 pm
Location: Carefully place an additional prawn on the barbecue


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 36 guests