You are using a version without Ads of this website. Please, consider donating:

### Sams Teach Yourself C++ in 24 Hours book questions

Im following the Sams Teach Yourself C++ in 24 Hours book and I have some general questions that i'll post here in this thread for convenience.

Ok so this is my first question, in the book there is this code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include using namespace std; class SimpleCat { public: SimpleCat(); ~SimpleCat(); int GetAge() const { return *itsAge; } void SetAge(int age) { *itsAge = age; } int GetWeight() const { return *itsWeight; } void setWeight(int weight) { *itsWeight = weight; } private: int *itsAge; int *itsWeight; }; SimpleCat::SimpleCat() { itsAge = new int(2); itsWeight = new int(5); } SimpleCat::~SimpleCat() { cout << "Deleting itsAge..." << endl; delete itsAge; cout << "Deleting itsWeight..." << endl; delete itsWeight; } int main() { SimpleCat *Frisky = new SimpleCat; cout << "Frisky is " << Frisky->GetAge() << " years old\n" << endl; Frisky->SetAge(7); cout << "Frisky is " << Frisky->GetAge() << " years old\n" << endl; delete Frisky; return 0; }``````

now in the code i am a little confused about this:

 ``1234`` ``````int GetAge() const { return *itsAge; }``````

Why is `const` after the parenthesis instead of before int or return?
if const were before the int it would apply to the returning number. The const after the parenthesis applys to the calling object.
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.
Whats the calling object? if itsAge is being returned to the function then why not just put const in front of int? That makes no sense to me.
Last edited on
"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.
 ``1234`` ``````//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

int GetAge( const Cat *this )
{
this->itsAge++; // compile error
return this->itsAge;
}
Im sorry but im still having a hard time understanding can you explain it in simpler terms?
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.
now in the code i am a little confused about this:

 ``1234`` ``````int GetAge() const { return *itsAge; }``````

Why isÂ constÂ after the parenthesis instead of before int or return?

It means the function won't modify the object (GetAge() doesn't change anything).
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?
Close. Consider this example:
 ``1234567891011121314`` ``````class MyClass{ public: int GetNumber()const; private: int mynum; }; int MyClass::GetNumber()const{ int anothernum = mynum; //Legal anothernum = 10; //Legal mynum = 10; //Illegal return mynum; }``````

It is the object which cannot be modified within the read-only function.
Last edited on
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 object that calls the function is constant within the function.
 ``12`` ``````Frisky.GetAge(); //Frisky becomes constant Adielle.GetAge(); //Adielle becomes constant ``````

The `*this` which exists in all member functions becomes const in read-only member functions.
 ``1234567`` ``````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){ return this->_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.

No, they have very different effects.
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````//Going to use "this" to show that the object is involved class Cat{ public: const int Age(int=0); Cat(const int); int GetAge(int=0)const; const int GetTheAge(int=0)const; const int TooManyConst(const int)const; private: int age; }; const int Cat::Age(int whatever){ //Only the returned value is constant (this->age)++; //Legal whatever++; //legal return this->age; } Cat::Cat(const int 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 return this->age; } const int Cat::GetTheAge(int whatever)const{ //Both the return value and this(object) are constant whatever++; //Legal //(this->age)++; //Illegal return this->age; } const int Cat::TooManyConst(const int whatever)const{//All three are constant //whatever++; //Illegal //(this->age)++; //Illegal return whatever+(this->age); }``````
Last edited on
 @Ch1156 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

 ``1234`` ``````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++)

So there is no difference between

 ``1234`` ``````int GetAge() { return *itsAge; }``````

and

 ``1234`` `````` const int GetAge() { return *itsAge; }``````

Last edited on
ok so why do people do the second part then? if there is no difference then why even do it?
Bump
Read the posts above. All was already said.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: