Why const is after that?


I'm new to C++ and I'm confused on something:

int GetNum () const;

Why const is after that? What is the difference between the code above and the one below:

int const GetNum ();

Thanks for your help

Kindest Regards
int const GetNum();

This function returns a const integer.

int GetNum() const;

The const after the function can only be used on member functions. These functions have an implicit hidden parameter, which is a pointer to the object:
int GetNum(/* Class *this */);

If the function is followed by const, the this pointer is const:
int GetNum(/* const Class *this */);

This means:
* You can't change member variables in a function declared as const.
* If an object is declared as const, you can only call functions that are declared const.
* From a const member function, you can only call other member functions that are also const.
Thanks a lot :)
Topic archived. No new replies allowed.