1、关于const的疑问
![这里写图片描述](https://img-blog.csdn.net/20171023212625496?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2、
![这里写图片描述](https://img-blog.csdn.net/20171023212638825?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
3、
![这里写图片描述](https://img-blog.csdn.net/20171023212652113?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
#include <stdio.h>
int main()
{
const int x = 1;
const int& rx = x;
int& nrx = const_cast<int&>(rx);
nrx = 5;
printf("x = %d\n", x);
printf("rx = %d\n", rx);
printf("nrx = %d\n", nrx);
printf("&x = %p\n", &x);
printf("&rx = %p\n", &rx);
printf("&nrx = %p\n", &nrx);
volatile const int y = 2;
int* p = const_cast<int*>(&y);
*p = 6;
printf("y = %d\n", y);
printf("p = %p\n", p);
const int z = y;
p = const_cast<int*>(&z);
*p = 7;
printf("z = %d\n", z);
printf("p = %p\n", p);
char c = 'c';
char& rc = c;
const int& trc = c;
rc = 'a';
printf("c = %c\n", c);
printf("rc = %c\n", rc);
printf("trc = %c\n", trc);
return 0;
}
结果:
x = 1
rx = 5
nrx = 5
&x = 0x7ffe89359240
&rx = 0x7ffe89359240
&nrx = 0x7ffe89359240
y = 6
p = 0x7ffe89359244
z = 7
p = 0x7ffe89359248
c = a
rc = a
trc = c
4、关于引用的疑问
![这里写图片描述](https://img-blog.csdn.net/20171023212705395?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
5、引用的本质是常量指针
![这里写图片描述](https://img-blog.csdn.net/20171023212718854?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
6、
![这里写图片描述](https://img-blog.csdn.net/20171023212730547?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
7、小结
![这里写图片描述](https://img-blog.csdn.net/20171023212743511?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenkxMzI3MDg2Nzc4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)