C++ const double pointer

C++ const double pointer

本文关键字:pointer double const C++      更新时间:2023-10-16

我想使恒定的双指针指向恒定指针指向恒定双重指针。我开始做到这一点(当然,我在书本上进行了一些搜索,并从头开始搜索了它),然后思考以下三种做法:

const double* cp; //pointer to a constant double
double *const cp; //constant pointer
const double *const cp; //constant pointer to a constant double

我认为下一步是写一个常量的双指针

double **const cp;// double constant pointer

然后我结合了最后两个语句,然后写

const double *const cp = arr[0];
double **const cp1 = arr ;

其中ARR是动态分配的双维数组。之后,我尝试验证我的所作所为,并写了以下陈述,期望产生错误。

**cp1 = 1;    // didn't produce error  
*cp1 = arr[4];    // didn't produce error
cp1 = new double*[5]; //produce error   

所以我无法制作上面描述的内容,一个恒定的双指针指向一个恒定的指针指向恒定的双重双重指针。我该怎么做?

预先感谢。

中只有一个 const
double **const cp1 = arr ;
//       ^^^^^

所以我不确定为什么您期望其他两个作业会产生错误。

如果您希望它是所有级别的const,则需要

const double *const *const cp1 = arr;
//                         ^ cp1 is ...
//                  ^ a const pointer to ...
//           ^ a const pointer to ...
// ^ a const double