Finally, I have an excuse to pull out this waste of $30 that I made 2 years or so ago... At least it'll finally have a use.
Alright, I found the example from the book, and the author doesn't actually address this point anywhere in the book itself. I'll leave it to others to explain it, since I personally don't know the answer either and would love to know.
"const" before the return type denotes that the value returned in constant. "const" after the parentheses denotes that the function is a read-only function. What Yanson points out is that the "const" qualifier applies to the current object with which you called the function, or *this.
Such as in:
Frisky->GetAge(); //const applies to *Frisky
Read-only functions guarantee that the object will be in no way modified from within the function. Read-only functions are especially useful for functions that take const object parameters.
1 2 3 4
//Just an example
void DiplayAge(const SimpleCat& dacat){
cout << dacat.GetAge() << endl;
}
Had GetAge() not been a read-only function, the compiler would spit out an error, complaining that a member function of a constant parameter has the chance of changing the object's state.
There is no any sense to declare this member function GetAge as
const int GetAge();
instead of
int GetAge() const;
because the stmantics of return types int and const int are the same. You may not for example to write
Frisky->GetAge() = 10;
for the function declared as int GetAge() const because the return value ia a temporary value that is rvalue and may not be assigned.
In fact any non-static member function has implicit first parameter
TheClass *this
So for class Cat method GetAge implicitly has declaration
int GetAge( const Cat *this );
where const before this is that const that is after the function in the original declaration
int GetAge() const;
This means that object pointed by this can not be changed. You may not for example define the function the following way
int GetAge() const
{
itsAge++; // compile error
return itsAge;
}
or that it was more clear I will rewrite the function with the explicit parameter
So in the function your returning a value, so why not just put the const before int GetAge()? you dont want the value to change so why put const after? im sorry i just dont get it.
Ok i think i might understand. So if the const is before the function then that means the return value cant be changed and if its after the parenthesis nothing within that block can be changed?
ok so is it just one object or any object within the function? Im aware of 3 different ways to use const, thats before the function type, before the function brackets, and before a variable. I dont understand the difference between any of them, what i understand is that if you want to make the variable constant you put const in front of it within the function body, which makes it constant, but that is wahts happening when you put it after the bracketsd isnt it? I just dont get it. To me it seems like 3 different ways to do the same exact thing but maybe im worng, please help me understand im so confused.
The *this which exists in all member functions becomes const in read-only member functions.
1 2 3 4 5 6 7
int SimpleCat::GetAge()const{
return _age;
}
//Is like (purely just a thought example; this won't compile at all)
int SimpleCat::GetAge(const SimpleCat *this){
returnthis->_age;
}
To me it seems like 3 different ways to do the same exact thing but maybe im worng, please help me understand im so confused.
//Going to use "this" to show that the object is involved
class Cat{
public:
constint Age(int=0);
Cat(constint);
int GetAge(int=0)const;
constint GetTheAge(int=0)const;
constint TooManyConst(constint)const;
private:
int age;
};
constint Cat::Age(int whatever){ //Only the returned value is constant
(this->age)++; //Legal
whatever++; //legal
returnthis->age;
}
Cat::Cat(constint newage){ //Only the parameter is constant
// newage++; //Illegal
this->age = newage; //Legal
}
int Cat::GetAge(int whatever)const{ //Only this(object) is constant
//(this->age)++; //Illegal
whatever++; //legal
returnthis->age;
}
constint Cat::GetTheAge(int whatever)const{ //Both the return value and this(object) are constant
whatever++; //Legal
//(this->age)++; //Illegal
returnthis->age;
}
constint Cat::TooManyConst(constint whatever)const{//All three are constant
//whatever++; //Illegal
//(this->age)++; //Illegal
return whatever+(this->age);
}
So in the function your returning a value, so why not just put the const before int GetAge()? you dont want the value to change so why put const after? im sorry i just dont get it.
It can not be changed if you even will omit const. For example let assume that the function has no the leading const qualifier
1 2 3 4
int GetAge()
{
return *itsAge;
}
then you may not write
SimpleCat Frisky;
Frisky.GetAge() = 10;
The compiler will issue an error (if the compiler has no a bug as MS VC++)