The following essay explains the C + + shared data protection mechanism.
Protection of shared data
Data that needs to be shared and prevented from changing should be declared as constant type (decorated with const).
Member functions that do not change the state of the object should be declared constant.
(1) Constant type
① Constant object: must be initialized and cannot be updated.
Const class name object name
② Regular member
Class members decorated with const: constant data members and constant function members
③ Constant reference: the referenced object cannot be updated.
Const # type specifier & reference name
④ Constant group: array elements cannot be updated (see Chapter 6 for details).
Type specifier const array name [size]
⑤ Constant pointer: pointer to constant (see Chapter 6 for details).
(2) Constant object
Object decorated with const
1 case:
2
3 class A
4
5 {
6
7 public:
8
9 A(int i,int j) {x=i; y=j;}
10
11 ...
12
13 private:
14
15 int x,y;
16
17 };
18
19 A const a(3,4); // A is a constant object and cannot be updated
(3) Regular member
Object members decorated with const
① Constant member function
Functions described with const keyword.
Constant member functions do not update the data members of an object.
Constant member function description format:
Type specifier: function name (parameter table) const;
Here, const is an integral part of the function type, so the const keyword should also be carried in the implementation part.
Const keyword can be used to distinguish overloaded functions
Through a constant object, only its constant member functions can be called.
② Constant data member
The data member described using const.
1 // examples of constant member functions
2
3 #include
4
5 using namespace std;
6
7 class R {
8
9 public:
10
11 R(int r1, int r2) : r1(r1), r2(r2) { }
12
13 void print();
14
15 void print() const;
16
17 private:
18
19 int r1, r2;
20
21 };
22
23
24
25 void R::print() {
26
27 cout << r1 << ":" << r2 << endl;
28
29 }
30
31 void R::print() const {
32
33 cout << r1 << ";" << r2 << endl;
34
35 }
36
37 int main() {
38
39 R a(5,4);
40
41 a.print(); // Call void print()
42
43 const R b(20,52);
44
45 b.print(); // Call void print() const
46
47 return 0;
48
49 }
1 // examples of constant data members
2
3 #include
4
5 using namespace std;
6
7 class A {
8
9 public:
10
11 A(int i);
12
13 void print();
14
15 private:
16
17 const int a;
18
19 static const int b; // Static constant data member
20
21 };
22
23
24
25 const int A::b=10;
26
27 A::A(int i) : a(i) { }
28
29 void A::print() {
30
31 cout << a << ":" << b <
(4) Often cited
If you use const when declaring a reference, the declared reference is a constant reference.
Objects referenced by frequent references cannot be updated.
If you use constant references as formal parameters, you won’t accidentally change the arguments. The frequently cited declaration forms are as follows:
Const , type specifier & reference name;
1 // often referenced as formal parameters
2
3 #include
4
5 #include
6
7 using namespace std;
8
9 class point {// point class definition
10
11 public: / external interface
12
13 Point(int x = 0, int y = 0)
14
15 : x(x), y(y) { }
16
17 int getX() { return x; }
18
19 int getY() { return y; }
20
21 friend float dist(const Point &p1,const Point &p2);
22
23 private: / private data member
24
25 int x, y;
26
27 };
28
29
30
31 float dist(const Point &p1, const Point &p2) {
32
33 double x = p1.x - p2.x;
34
35 double y = p1.y - p2.y;
36
37 return static_cast(sqrt(x*x+y*y));
38
39 }
40
41
42
43 int main() {// main function
44
45 const Point myp1(1, 1), myp2(4, 5);
46
47 cout << "The distance is: ";
48
49 cout << dist(myp1, myp2) << endl;
50
51 return 0;
52
53 }