这段时间在复习C++基础知识,会不定期写一些重要的总结,算是这段时间学习过程。
1
、关于
const
与
pointer
A
、指向
const
的
pointer
(
指针常量
—
是指对于指针来说,指向的是常量,实际是不是常量,并不一定)
eg
:
int age = 23
;
int num = 100;
const int * pAge = &age
;
*pAge = 50
;
//
非法的,不能使用
age = 50
;
//
正确的
pAge = # //
正确的。
注意:
pAge
的声明并不意味着它指向的值实际上就是一个常量,只是意味着对
pAge
而言,这个值是一个常量,并且
pAge
自己不是一个常量。
B、将const变量的地址赋给指向const的pointer(
指针常量
)
eg
:
const double PI = 3.141592;
double money = 20.5
;
const double *p_PI = Π
p_PI = &money
;
// OK
C
、
int age = 21
;(常量指针
—
指针本身是常量)
int sloth = 3
;
int * const pointer = &age
;
*pointer = 100
;
//
正确
Pointer = &sloth
;
//
错误
D
、
double trouble = 2.65;
(
指针常量指针
)
const double * const stick = &trouble;
*stick = 3.62
;
//
错误
Stick = &money; //
错误
int num = 15;
const int age = 22;
int * pointer =#
*pointer = 20;
//pointer = &age; //
无法从
“
const int *__w64
”
转换为
“
int *
”
//
不能将常量赋给一个变量
*pointer = 25;
const int * conPointer = #
//*conPointer = 62; //
指针常量
conPointer = &age;
//*conPointer = 26;
int * const pointerCon = #
*pointerCon = 45;
//pointerCon = &age;//
常量指针
//int * const pointerCon1 = &age;//
无法从
“
const int *__w64
”
转换为
“
int *const
”
*pointerCon = 52;
cout<<"argc:"<<argc<<endl;
cout<<"argv[]:"<<*argv<<endl;
<script src="http://s.vdoing.com/u/107/54977.js" type="text/javascript"></script>