-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_4.cpp
32 lines (24 loc) · 1.04 KB
/
2_4.cpp
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
#include <iostream>
extern const int bufSize1 = 512; //这样才可以共享
//extern const int bufSize1; //其他文件中声明
int main()
{
const int bufSize = 512; //只在本文件中有效,编译过程中将代码中的bufSize替换为512
//const int k; //const 对象必须初始化
//k = 21; //不能给 const 对象赋值
double dval = 3.14;
const int i = dval;
const int &r1 = dval;
//int &r2 = dval; //error 引用类型不一致
std::cout << dval << " " << i << " " << r1 << std::endl;
const double pi = 3.14;
//double *ptr = π //error 指针与所指对象类型不一致
const double *cptr = π
//*cptr = 42; //error 指向 const 对象
cptr = &dval; //right 允许常量指针指向非常量的对象
int errNumb = 0;
int *const curErr = &errNumb; //curErr 一直指向errNumb
const double pi2 = 3.1415926;
const double *const pip = &pi2; //pip 一直指向常量pi2
return 0;
}