University of Michigan at Ann Arbor
Last Edit Date: 01/29/2023
Disclaimer and Term of Use:
We do not guarantee the accuracy and completeness of the summary content. Some of the course material may not be included, and some of the content in the summary may not be correct. You should use this file properly and legally. We are not responsible for any results from using this file
This personal note is adapted from Professor Amir Kamil, Andrew DeOrio, James Juett, Sofia Saleem, and Saquib Razak. Please contact us to delete this file if you think your rights have been violated.
This work is licensed under a Creative Commons Attribution 4.0 International License.
const
Keyword¶The const
keyword is a type quantifier that we can add to declarations to tell the complier that we do not intend for some value to change or be changeable.
const
forbids assignment. However, const
can be used in initialization.
Declaration of const
:
int const * arr[6];
(arr
is an array of 6 pointers to int const)
const int * arr[6];
(arr
is an array of 6 pointers to const int)
The two declations above work the same.
const
pointer: The pointer value (an address) itself cannnot change.
1 int x = 3; 2 int * const ptr = &x;
Try it out
Pointer-to-const
: The object which pointed by the pointer cannot change.
1 int x = 3; 2 int const * ptr = &x; 3 const int * ptr = &x;
Try it out
Reference-to-const
1 int x = 3; 2 int &y = x; 3 y = 10; // legal, changes x too
Try it out
int x = 3; int const &y = x; y = 10; // error x = 10; // still legal
Try it out
const
Conversions¶The compiler will only allow a function to be called on a const
object if the parameters also include the necessary const
qualification to continue protecting that object. Essentially, only functions that "promise" not to change their parameters are allowed to be called on const
objects/variables.
The following is an example about const
conversions.
1 void array_func_1(const int *arr); 2 void array_func_2(int *arr); 3 void int_func_3(int a); 4 5 int main() { 6 const int arrA[4] = {1, 3, 2, 6}; 7 int arrB[3] = {5, 5, 5}; 8 const int num = 3; 9 10 array_func_1(arrA); // ok 11 array_func_1(arrB); // ok 12 array_func_2(arrA); // error 13 array_func_2(arrB); // ok 14 int_func_3(num); // ok 15 }
We can define a new compound object type via a struct definition.
The strut
definition contains member variable declarations.
We define objects as instance of that type and use them in the program.
Individual members are accessed vias the .
operator (also called the "member access operator").
If a struct
object is const-qualified, that forbids assignment to the struct as a whole and also forbids assignment to its individual members. That fits with the idea of const as "this shouldn't change".
pointer
and struct
For example, assume obj
is a Person
object and ptr
is a Person*
pointer that points to that object. Then you would access the person's age as either:
obj.age
ptr->age
Use the .
for objects and ->
for pointers.
Example:
Try it out
struct
¶If you need to modify the original object, use pass-by-pointer or pass-by-reference.
If you don't modify the original object, use pass-by-pointer-to-const or pass-by-reference-to-const. This protects against accidental modification but more importantly also ensures your function can actually be called on const objects.
Only use pass-by-value for fundamental objects (e.g. int
, double
, etc.) or very small compound objects. If the objects are large (e.g. string
, vector
, your own custom struct
s, etc.), pass-by-value makes an expensive and unnecessary copy.