C++ Library Extensions
2022.12.09
To help learn modern C++ programming
000-pointer.cpp
Go to the documentation of this file.
1
#include <iostream>
2
3
/*
4
In C++, we always have to INITIALIZED const and reference entities.
5
6
Once again, we always have to INITIALIZED const entities and reference entities.
7
8
An entity is an object or not an object.
9
10
An object in C++ has its own memory.
11
12
ONCE again, if you remember that const entities and reference entities should
13
always be initialized at definition.
14
15
If you do not initialize at definition, that is neither a const nor a reference.
16
*/
17
18
void
examples_for_const_pointer
()
19
{
20
int
n;
// we have not initialized
21
// n is neither const nor reference
22
23
// const int c; // syntax error.
24
// because const entity should be initialized
25
// but not initialized at definition
26
27
const
int
c = 5;
// c is an entity, not yet an object
28
// in C++, const entity may or may not exist at run-time.
29
30
/*
31
In C++, keyword "const" was first introduced to replace macros, which are
32
type-agnostic or type-ignorant.
33
34
Anything that should be initialized at definition is in some way CONSTANT.
35
36
That is, all reference types are constant... because they should always be
37
initialized at definition.
38
*/
39
40
// int& r; // syntax error, because reference is CONSTANT
41
int
& r = n;
42
43
// const int& cr; // syntax error, because reference is CONSTANT
44
45
const
int
& cr = n;
// what does "const" in "const int&" means?
46
47
/*
48
"const" int "const int&" does not mean the reference is constant.
49
because all reference in C++ are already constant.
50
"const" in "const int&" does not mean cr can reference to const int.
51
52
"const" in "const int&" MEANS the value that the reference cr
53
references to cannot be modified.
54
55
Once again, "const" in "const int&" DOES NOT MEAN
56
the reference cr references to "const int", but it rather means
57
the value that the reference cr references to is NOT MODIFIABLE.
58
59
Understanding this point is REALLY IMPORTANT.
60
*/
61
62
// cr = 5; // syntax error, because cr references to an int n
63
// but it was declared with const int&,
64
// the "const" in "const int&" means
65
// the value cr references to cannot be modified.
66
67
68
// All references in C++, be it lvalue reference or rvalue reference,
69
// they cannot be modified once declared and initialized.
70
71
double
d1, d2;
72
73
double
& rd1 = d1;
74
75
rd1 = d2;
// ? what does it mean?
76
// it means, copy the value of d2 to the object d1 to which the
77
// reference rd1 references.
78
// Here, we copied the value and assigned to rd1, which
79
// references to d1, we modified the value the reference rd1 references to.
80
// We have NOT modified the value of rd1.
81
// We changed the value that the reference rd1 references to.
82
83
// rd1 = &d2; // it is syntax error, the ampersand & is address-of operator,
84
// which returns the address of d2,
85
// we cannot assign the address of d2 to rd1.
86
rd1 = (
int
&) d2;
// this is not a syntax error,
87
// but rd1 = (int&)d2 is equivalent to rd1 = d2,
88
// that is, we copied the value of d2 to the object
89
// that the reference rd1 references to, which is
90
// d1.
91
92
/*
93
Once and for all, all reference types is initialized at definition,
94
and cannot reference to something else.
95
In this sense, all references are CONSTANT, which does not mean
96
we cannot change the value that the reference references to.
97
98
Do not get confused.
99
*/
100
101
float
f1 ;
102
const
float
cf = 5.0f;
// At this point, cf is NOT an object,
103
// that is, no memory is allocated for cf.
104
// keyword "const" was originally introduced to C++
105
// to replace type-agnostic #define macros.
106
// Initial C++ design for keyword "const"
107
// was meant to be used in place of #define macros
108
109
const
float
& cfr1 = f1;
// "const float&" can be initialized with non-const float
110
const
float
& cfr2 = cf;
// "const float&" can be initialized with const float
111
112
float
& rf1 = f1;
// okay, "float&" can be initialized with non-const float
113
// float& rf2 = cf; // syntax error. we cannot normally initialize
114
// "non-const" reference, float&, with const float.
115
/*
116
If we want to initialize "non-const" reference with const entity,
117
we HAVE TO USE type cast, const_cast<type&>(entity) syntax, as below:
118
*/
119
120
float
& rf2 =
const_cast<
float
&
>
(cf);
// PERFECT.
121
// Now at this point, some magic happens.
122
// Since const float cf is now "referenced"
123
// here "referenced" means the address of the
124
// "referenced" entity is required, so
125
// C++ compiler generates memory for
126
// const float cf. From now on, cf is
127
// a full-blown C++ object, because it now has its
128
// own memory.
129
130
/*
131
In the C++ Community, the colloquial term "non-const reference" means that
132
we can alter the value the reference is referencing to. It does not mean
133
the reference is referencing to a non-const object.
134
135
In the same manner, the colloquial term "const reference" means
136
we cannot alter the value the reference is referencing to. It does NOT mean
137
the reference is referencing to a const object.
138
139
In C++ Language and Community, misnomers and ill-coined technical terms are
140
ABUNDANT. Possibly because of historical reasons. For example, "volatile" objects
141
are not volatile at all. It rather means the CPU should read or fetch the
142
value of the object from MEMORY (or RAM) each time it needs access to it.
143
144
"Const expression" rather means "compile-time expression."
145
146
147
*/
148
149
}
150
151
int
main
()
152
{
153
examples_for_const_pointer
();
154
}
examples_for_const_pointer
void examples_for_const_pointer()
Definition:
000-pointer.cpp:18
main
int main()
Definition:
000-pointer.cpp:151
CppExtension
examples
000-pointer.cpp
Generated by
1.9.4