cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Why const is after that?
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

question  Why const is after that?

GBStudio (6)
Hi

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
GBStudio
|
ropez (312)
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.
|
GBStudio (6)
Thanks a lot :)
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us