是否可以在数组上使用const_cast来更改元素?

时间:2017-07-30 02:53:55

标签: c++ arrays pointers const-cast

这是一个学术问题,因为我知道通常会避免const_cast

但是我正在进行第3章的练习,#27,Thinking in C ++,Vol。 1。

  

创建一个double的const数组和一个double的volatile数组。指数   通过每个数组并使用const_cast将每个元素转换为   分别为非const和非volatile,并为每个赋值   元件。

我看到如何const_cast单个变种:

const int i = 0;
int* j = const_cast<int*>(&i);

*j = 1;    // compiles and runs

但无法弄清楚如何让它与数组一起工作。以下编译,但抛出“错误访问”异常,好像const仍在那里。

const int sz = 10;
const double cd[sz] {0,1,2,3,4,5,6,7,8,9};
volatile double vd[sz] {0,1,2,3,4,5,6,7,8,9};

double* cdp = const_cast<double*>(cd);
double* vdp = const_cast<double*>(vd);

cdp[0] = 99;    // bad access error

我哪里弄错了?

1 个答案:

答案 0 :(得分:2)

请注意:

const int i = 0;
int* j = const_cast<int*>(&i); 

*j = 1;    // UNDEFINED BEHAVIOR - modify actually const data

允许通过丢弃最初声明为const的constness来修改对象。编译器可以将对象(或数组,在您的情况下)的内容放在只读存储器中,该存储器在机器中比编译器更低级别强制执行。写入时,可能会触发故障。如果它被声明为const,你必须永远尊重它,或者在遇到崩溃时崩溃。

如果对象最初被声明为非const,则在抛弃constness之后只能 修改。 (也就是说,后来添加了constness,可能作为函数的参考参数。)

为了举例,让我们将你的问题重新解释为至少一个有效的情况。

// f() takes a reference to an array, but add constness
void f(const double(&arr)[10]) 
{
    // Here it's ok to cast away constness and modify since the 
    // underlying object isn't declared const (though in general a
    // function doesn't know that about its caller, except in 
    // constrained situations.)
    double * array = const_cast<double*>(arr);
    array[1] = 99;
}

int main() 
{
    // NOTE: array is NOT CONST
    double arr[10] {0,1,2,3,4,5,6,7,8,9};
    f(arr);
}

这很好。

相关问题