meaning of the const keyword appended to declarations

Hi, I'm looking at some of the overloaded declarations of member functions in classes.

They have a const, which I know means constant, appended to the end of the declaration. Can someone fill me in on what this means?

I know it means that if it begins with const... it's a constant... but I don't see why you would append one after the parameters in a declaration
example:

int compare ( const string& str ) const;
I'm used to seeing the type following the const keyword... meaning the variable stays the same always and can't be changed...
does that simply m ean they have to be tested for equality with a constant?
const keyword has a few uses in C++.

For your question you are asking the one in bold correct ? int compare ( const string& str ) const;

I only know their use if int compare is a member function inside a C++ class.

e.g
class Test {
public:
int compare(const string& str) const;
int compare(const string& str); //usually this version is provided also for callee to use
...
}

Above indicate compare member function can be called only by a constant object of class Test;

e.g
const Test i_am_constant; //assume default constructor is provided
//from here onwards i_am_constant if use it's member function are NOT allowed to modify the data members inside itself.

Think of it like const int i_am_constant_int = 0;
//from here onwards i_am_constant_int value cannot be changed any longer
thanks... that makes perfect sense :)
Topic archived. No new replies allowed.