#include <iostream>
void f(const int * & p)
{
int i =0;
i = p[0];
std::cout << i << std::endl;
}
int main()
{
int * p =new int[1];
p[0] =102;
f(p);
return 1;
}
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 f(const int*&)’
하지만
void f(const int * const & p)
모두에 "F"기능을 변경하면 괜찮습니다. 누군가 const가 왜 이런 식으로 행동하는지 설명 할 수 있습니까? 감사.
안녕. 이 경우 항상 표시되는 오류 메시지를 게시하는 것이 좋습니다. 제발 그렇게 할 수 있니? – razlebe
http://duramecho.com/ComputerInformation/WhyHowCppConst.html 왜이 오류가 발생하는지이 참조를 확인하십시오. –
gcc4.3.4에 의해 생성 된 오류 메시지를 추가했습니다. – razlebe