C++ Library Extensions 2022.12.09
To help learn modern C++ programming
000-constptr.cpp
Go to the documentation of this file.
1#include <iostream>
2
4{
5 int n = 5;
6
7 int* np1; // np1 is declared int*, where the star * , or is called
8 // asterisk is a declarator, or a pointer declarator
9 // since np1 is not initialized, it is not a const pointer
10
11 int& ref = n; // initialize reference with n
12
13 // SSTS: const int* is not "CONSTANT" pointer, but we call it just "constant pointer"
14
15 const int* cp1; // is this a constant pointer?
16 // since we did not initialize, the VSC does not not squiggle
17 // cp1 is not a const pointer
18
19 const char* msg; // it is not a const pointer either.
20
21 int * ptr; // the star * means ptr is a type of pointer.
22 // "int" in front of the star * has 3 implications;
23 // 1. Arithmetic operation over ptr is based of size(int),
24 // which happens to be 4 bytes.
25 // ptr+1 means add 4 (bytes) to the value of ptr
26 // ptr+2 means add 8 (bytes) to the value of ptr
27 // 2. when the pointer object ptr is dereferenced using star *
28 // this star * is an operator, dereferencing operator
29 // "int" in front of star * instructs the CPU
30 // read or fetch 4 bytes from memory.
31 // 3. "int*" instructs the C++ compiler to implicitly
32 // implement array indexing operator []
33
34 ptr = &n; // we assigned the address of n to the value of ptr
35
36 // the expression ptr+1 is not the same to *ptr + 1
37
38 const int& crf = 6;
39
40 const int& ccref = n;
41
42
43 std::cout << "The address of n : " << &n << std::endl;
44 std::cout << "The address of ref: " << &ref << std::endl;
45 std::cout << "The value of ref : " << ref << std::endl;
46 std::cout << "The address of ptr: " << &ptr << std::endl;
47 std::cout << "The value of n : " << n << std::endl;
48 std::cout << "The value the ptr pointing to: " << *ptr << std::endl;
49 std::cout << "The value of the ptr: " << ptr << std::endl;
50 std::cout << "The address of crf: " << &crf << std::endl;
51 std::cout << "The value of crf (or it references to): " << crf << std::endl;
52
53 std::cout << "The address of ccref: " << &ccref << std::endl;
54 std::cout << "The value of ccref (or it references to): " << ccref << std::endl;
55
56 /*
57 An object in C++ has special meaning.
58 It means... it has its own independent memory.
59 */
60
61 double d1 = 5.0;
62 const double cd = 6.0;
63
64 double* dp1 = &d1;
65 double* dp2 = &const_cast<double&>(cd); // If we want to assign the address of const double to
66 // a pointer of type double*, then we 1. have to remove the constness of the const double
67 // using typecase const_cast<double&>(cd), 2. now that the address using address-of operator &
68 // & const_cast<double&>(cd)
69
70 const double* cdp1 = &d1;
71 const double* cdp2 = &cd;
72
73 cdp2 = &d1; // we modified the value of cp2 with a new address of another object of type double.
74 // since we modified the value of cp2, "const" in "const double*" does not mean
75 // the pointer cp2 is CONSTANT.
76
77 // *cp1 = 6.7; // this does not work.
78 // the dereference operator * in front of cp1, the pointer, means
79 // the value of the object the pointer cp1 is pointing to.
80 //
81 // cp1 - is the value of cp1, which is the address of d1, or &d1
82 // *cp1 - is the value of the object that the pointer is pointing to,
83 // in this case, the value of d1
84
85 // The question now is what does "const" in "const double*" mean?
86
87 /*
88 1. "const" in "const double*" does NOT mean we can only assign the address of the const object of type double.
89 2. "const" in "const double*" does NOT mean we cannot assign a new address of another object of type double.
90 that is, the pointer of type "const double*" is NOT const pointer.
91
92 "const" in "const double*" means that we cannot modify the value of the object
93 the pointer is pointing to. It does not mean we cannot modify the value of the pointer itself.
94 */
95
96 int m = 5;
97
98 int* mptr = &m;
99
100 *mptr = 6; // it works successfully.
101
102 const int* cmptr = &m;
103
104 // *cmptr = 7; // it does not work.
105
106 /*
107 Colloquial terms in C++ Community:
108
109 const int* ptr = &n;
110
111 Strictly speaking, technically speaking or SSTS, ptr is not a CONSTANT pointer, but we say "constant pointer",
112 we usually mean a pointer declared with const type* syntax.
113
114 SSTS, *ptr is NOT the value of ptr, or the value of the pointer. It is the value of the object
115 that the pointer is pointing to. But in C++ Community, we commonly call it "the value of the pointer"
116
117 The Korean language is ECONOMIC, SCIENTIFIC, AND friendly to new technology and science.
118
119 포인터값: just 4 syllables correctly deliver the meaning of the "value of the pointer"
120 포인터 참조값: just 6 syllables correctly mean "the value of the object the pointer is pointing to."
121
122 In this sense, Korean is science and technology friendly, which sadly is not with the English language.
123 */
124}
125
126int main()
127{
129}
void examples_for_pointer_to_const_object()
Definition: 000-constptr.cpp:3
int main()
reference_wrapper< Type > ref(Type &val) noexcept
auto & cout
auto & endl