const Keyword in C++
Constant is something that doesn't change. In C language and C++ we use the keyword const
to make program elements constant. const
keyword can be used in many contexts in a C++ program. It can be used with:
- Variables
- Pointers
- Function arguments and return types
- Class Data members
- Class Member functions
- Objects
1) Constant Variables in C++
If you make any variable as constant, using const
keyword, you cannot change its value. Also, the constant variables must be initialized while they are declared.
In the above code we have made i
as constant, hence if we try to change its value, we will get compile time error. Though we can use it for substitution for other variables.
2) Pointers with const
keyword in C++
Pointers can be declared using const keyword too. When we use const
with pointers, we can do it in two ways, either we can apply const
to what the pointer is pointing to, or we can make the pointer itself a constant.
Pointer to a const
variable
This means that the pointer is pointing to a const
variable.
Here, u
is a pointer that can point to a const int
type variable. We can also write it like,
still it has the same meaning. In this case also, v
is a pointer to an char
which is of const
type.
Pointers to a const
variable is very useful, as this can be used to make any string or array immutable(i.e they cannot be changed).
const
Pointer
To make a pointer constant, we have to put the const
keyword to the right of the *
.
Here, w
is a pointer, which is const
, that points to an int
. Now we can't change the pointer, which means it will always point to the variable x
but can change the value that it points to, by changing the value of x
.
The constant pointer to a variable is useful where you want a storage that can be changed in value but not moved in memory. Because the pointer will always point to the same memory location, because it is defined with const
keyword, but the value at that memory location can be changed.
NOTE: We can also have a const
pointer pointing to a const
variable.
3) const
Function Arguments and Return types
We can make the return type or arguments of a function as const
. Then we cannot change any of them.
Some Important points to Remember
- For built in datatypes, returning a
const
or non-const value, doesn't make any difference.
- Both
j
and k
will be assigned the value 1
. No error will occur. - For user defined datatypes, returning
const
, will prevent its modification. - Temporary objects created while program execution are always of
const
type. - If a function has a non-const parameter, it cannot be passed a
const
argument while making a call.
- If we pass a
const int*
argument to the function t
, it will give error. - But, a function which has a
const
type parameter, can be passed a const
type argument as well as a non-const argument.
- This function can have a
int*
as well as const int*
type argument.
4) Defining Class Data members as const
These are data variables in class which are defined using const
keyword. They are not initialized during declaration. Their initialization is done in the constructor.
In this program, i
is a constant data member, in every object its independent copy will be present, hence it is initialized with each object using the constructor. And once initialized, its value cannot be changed. The above way of initializing a class member is known as Initializer List in C++.
5) Defining Class Object as const
When an object is declared or created using the const
keyword, its data members can never be changed, during the object's lifetime.
Syntax:
For example, if in the class Test
defined above, we want to define a constant object, we can do it like:
6) Defining Class's Member function as const
A const
member function never modifies data members in an object.
Syntax:
Example for const Object and const Member function
Falcon has left the Base Falcon has left the Base 10 20
Here, we can see, that const
member function never changes data members of class, and it can be used with both const
and non-const objecta. But a const
object cannot be used with a member function which tries to change its data members.
mutable
Keyword
mutable
keyword is used with member variables of class, which we want to change even if the object is of const
type. Hence, mutable
data members of a const
objects can be modified.
正好也可以学一学英文 比较方便-- 就是这种带示例的 比较方便大家理解,而且相对没那么多的错误