const correctness has me somewhat confused.
What rule of thumb do you use to decide when something should be const or not?
e.g. consider this example
class MyClass
{
string ToString(); // this one?
const string& ToString(); // or this?
const string& ToString() const; // or this?
char* ToString(); // What about this?
const char* ToString(); // or this?
const char* ToString() const; // or this?
const char const* ToString(); // Is this legal?
const char const* ToString() const; // how about this?
char const* ToString(); // or even this?
};
Const can get really confusing.
What would be the difference between all these ToString methods?
If I understand correctly, the first one returns a new string object that can be modified if need be. The second one returns a constant reference maybe it should be string const& ToString(). The third one is probably a nonsense because references are always constant is that correct?
Thought I'd throw the old char* versions in there for comparison as I do have methods that return object pointers and I'm not sure whether they should be const or not.
I guess I'm just trying to understand the limitations and benefits of const correctness and how to decide up front whether something should be const or not and how to correctly apply const since placing const in different places changes the meaning.
EDIT: also, how do I deal with that '... discards qualifiers'. What does that actually mean?