我正在尝试学习 C++,但在学习指针和引用时遇到了一些困难。我试图理解为什么以下某些内容不起作用,我似乎无法弄清楚“double *const ptd”和“const double *ctd”之间的区别
double d;
const double r; //bad; r must be initialised
const double pi = 3.1416;
double *ptr = π //illegal to point to a constant, because otherwise one could change the value of the constant which defies the purpose of a constant
double *const cpt; //bad; cpt must be initialised
double *const ptd = &d;
const double *ctd = &d;
const double *ptc = π
double *const ptp = π //illegal
const double *const ppi = π
double * const * pptr1 = &ptc;
double * const * pptr2 = &ptd;
void F () {
ptr = new double;
r = 1.0;
*ptr = 2.0;
cpt = new double;
*cpt = 3.0;
ptc = new double;
*ptc = 4.0;
ptd = new double;
*ptd = 5.0;
ctd = new double;
*ctd = 6.0;
ptp = new double;
*ptp = 7.0;
ppi = new double;
*ppi = 8.0;
}