#include <iostream>usingnamespacestd;intmain(){constinta=5;constint*p=&a;int*p_var=NULL;p_var=const_cast<int*>(p);//强转为非const指针cout<<a<<endl;*p_var=10;//重新赋值cout<<"*p="<<*p<<endl;//输出10cout<<"*p_var="<<*p_var<<endl;//输出10cout<<"a="<<a<<endl;//输出...
const指针原则上是不能转换成非const指针的,不过C++语言提供了一个强制类型转换符号const_cast,来实现强制转换,用法:const char *p;const_cast<char*>(p)强制类型转换还是要少用。
是的,那我就应该去调用const Getiterator吧?但是我没有提供const版本Getiterator 之后编译器就想试试可不可以把const 迭代器引用转换为非const迭代器引用(隐式类型转换),这肯定是语法不允许的,这属于权限的放大,自然就报了一个不能转换为非const迭代器的报错了。 解决方案: 了解了报错原因,我们只需要提供一份const...
*pc = 'C'; // would allow to modify a const object