Asaf
Sleep Deprived
- Joined
- Mar 22, 2010
- Messages
- 1,326
In C++, const has several usages.
When used for variables, then yes - it means they're not really variables but have constant values which cannot be changed (there are more options there with regards to pointers, but let's really not go there right now).
You can also define methods (= member functions) as const. That means that you declare them as 'not changing the instance for which you called this method'.
Technically what it means, is that the 'this' pointer which this method receives (and with which you actually call other methods and use variables of this instance) is also const, so you cannot:
1. Modify member variables
2. Call other non-const methods, because they might change member variables values.
const is a type 'qualifier' because it is considered something that modifies an existing type to another type. non-const variables can be converted implicitly to the const qualified type (because you only add a restriction), but the opposite does not apply.
So if you have a const method, and from it you try to call a non-const method, the compiler cannot implicitly convert the current const instance to a non-const instance.
I hope I didn't complicate you too much...
When used for variables, then yes - it means they're not really variables but have constant values which cannot be changed (there are more options there with regards to pointers, but let's really not go there right now).
You can also define methods (= member functions) as const. That means that you declare them as 'not changing the instance for which you called this method'.
Technically what it means, is that the 'this' pointer which this method receives (and with which you actually call other methods and use variables of this instance) is also const, so you cannot:
1. Modify member variables
2. Call other non-const methods, because they might change member variables values.
const is a type 'qualifier' because it is considered something that modifies an existing type to another type. non-const variables can be converted implicitly to the const qualified type (because you only add a restriction), but the opposite does not apply.
So if you have a const method, and from it you try to call a non-const method, the compiler cannot implicitly convert the current const instance to a non-const instance.
I hope I didn't complicate you too much...