【发布时间】:2012-09-26 10:53:21
【问题描述】:
这是一个代码 sn-p,希望能传达我想要做的事情:
void updatePointer(const int*& i)
{
i++;
}
int main() {
int array[5];
int* arrayPtr = array;
updatePointer(arrayPtr );
return 0;
}
这会导致编译器错误:
prog.cpp: In function ‘int main()’:
prog.cpp:16: error: invalid initialization of reference of type ‘const int*&’ from
expression of type ‘int*’
prog.cpp:5: error: in passing argument 1 of ‘void updatePointer(const int*&)’
【问题讨论】:
-
我认为你根本无法获得对 const 的非 const 引用
-
我决定删除我的答案,因为我不确定你在问什么。
-
我基本上是在问为什么上面的代码编译不出来
-
问题很明确,在标题中。但是,该示例并未反映问题......并且该函数的主体显然违反了 OP 想要实现的目标。老实说,这个问题是以一种愚蠢的方式提出的。更重要的是,如果您问为什么代码无法编译。如果您更正示例以反映您的问题,由于完全不同的问题,它仍然无法编译。 =)
-
我不同意 - 我想要一个可以修改指针但不能修改指针指向的函数。
标签: c++ pointers reference constants