forked from slam-code/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch3.cc
More file actions
executable file
·103 lines (76 loc) · 2.11 KB
/
Copy pathch3.cc
File metadata and controls
executable file
·103 lines (76 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "all.h"
/*
* 尽可能使用const
*
* */
class Widget2;
char greeting[] = "ch3";
char *p = greeting;
const char *pp = greeting;
char *const ppp = greeting;
const char *const pppp = greeting;
void f0(const Widget2 *pw); //2种方式作用相同。等价
void f1(Widget2 const *pw);
void f2()
{
std::vector<int> vec;
const std::vector<int>::iterator iter = vec.begin();
*iter = 10; //ok,改变iter指向的内容
// ++iter;//错误,iter 是const
std::vector<int>::const_iterator citer = vec.begin();
//*citer=10; //错误,citer指向的是const对象
++citer; //ok ,citer本身不是const
}
class Rational
{
};
const Rational operator*(const Rational &hls, const Rational &rhs);//返回const对象
//p19
class TextBlock
{
public:
TextBlock(std::string s)
{
text = s;
}
const char &operator[](std::size_t position) const
{
std::cout << "\n调用const版本[]: ";
return text[position];
}
char &operator[](std::size_t position)
{
std::cout << "\n调用non-const版本[]: ";
return text[position];
}
private:
std::string text;
};
void f3()
{
TextBlock tb("hello");
std::cout << tb[0]; //重载了[]运算符
const TextBlock ctb(" world"); //调用const版本
std::cout << ctb[1];
}
void f4(const TextBlock &ctb)
{
// ctb[0]='y'; //错误,ctb[0]返回const对象,故不能修改
std::cout << ctb[0] << ctb[1] << ctb[2];
};
void f5(TextBlock &tb)
{
tb[0] = 'Z'; //正确,tb[0]返回非const对象,string[0]可修改!
//std::cout << std::endl<<tb[0]<<std::endl<<tb[1]<<std::endl<<tb[2];
std::cout << tb[0] << tb[1] << tb[2] << std::endl;
printf("%c ,%c ,", tb[0], tb[1]);
printf("%c ,%c ,", tb[2], tb[3]);
}
int main()
{
f3();
TextBlock tb("hello");
f4(tb);
f5(tb);
return 0;
}