I have a method that adds two rational classes. I don't understand the purpose of second const. I know the first constant is initializing the constant rational class, but dont know what the second constant is used for.b

Rational Rational :: plus(const Rational&r2)const
The trailing const is used to signify that the function doesn't change the data inside the class.
Non-static member functions have implicit parameter of type SomeClass * that inside function bodies is named as this. Placing trailing const for a non-static member function means that this implicit parameter has type const SomeClass *.
When inside a body of some non-static member function you access data members of the class in fact each member is prefixed by the pointer this.

Let assume that someVariable is a data member of some class. When you write inside the body of a non-static member function, for example,

someVariable = 10;

this is equivalent to

this->someVariable = 10;

So if the function has trailing qualifier const it means that this qualifier is applied to this and you can not write this->someVariable = 10.
Topic archived. No new replies allowed.