Redian新闻
>
southwest的点数expire嘛?
avatar
southwest的点数expire嘛?# Money - 海外理财
i*t
1
reference to pointer问题
void f(const int * & p)
{
int i =0;
i = p[0];
cout<< i<system("PAUSE");
}
int main()
{
int * p =new int[1];
p[0] =102;
f(p);
return 1;
}
这样编译不过去
error C2664: 'f' : cannot convert parameter 1 from 'int *' to 'const int *&'
如果吧函数改一下就可以了void f( int * & p) 或者void f( int * p)
都没问题
奇怪为啥最开始那个不行呢?
avatar
b*n
2
谢谢!
avatar
r*y
3
我试了一下, 用 const int * const & 也行。
不过还是不明白。

【在 i******t 的大作中提到】
: reference to pointer问题
: void f(const int * & p)
: {
: int i =0;
: i = p[0];
: cout<< i<: system("PAUSE");
: }
: int main()
: {

avatar
r*b
4
yes, 2 year from last activity.
avatar
r*e
5
按理说 const & 跟 & 是等价的
因为引用一旦初始化就不能被改变了,本身就是const

【在 r*******y 的大作中提到】
: 我试了一下, 用 const int * const & 也行。
: 不过还是不明白。

avatar
i*s
6
定义时用template就没问题。
我怀疑是C++ compiler 在解析时优先考虑的一个const int *的指针的引用,而不是一
个const (int *指针的引用),解析顺序问题。
template
void f(const T & p)
...

【在 i******t 的大作中提到】
: reference to pointer问题
: void f(const int * & p)
: {
: int i =0;
: i = p[0];
: cout<< i<: system("PAUSE");
: }
: int main()
: {

avatar
n*0
7
const int * &p 里面的const 是修饰int 的。所以开始的定义说的是p指向的是const
int,也就是只能读不能改的int。由于main里面p定义成的是一个内容是int 的数组,
内容是可改的,所以调用f的时候编译器就报错了。
俺的理解。。。

【在 i******t 的大作中提到】
: reference to pointer问题
: void f(const int * & p)
: {
: int i =0;
: i = p[0];
: cout<< i<: system("PAUSE");
: }
: int main()
: {

avatar
d*i
8
main里的int 数组不是const型
avatar
i*t
9
i don't think so.
My friend can compile without error
I guess it's the problem of different compiler that cause the problem.

const

【在 n*******0 的大作中提到】
: const int * &p 里面的const 是修饰int 的。所以开始的定义说的是p指向的是const
: int,也就是只能读不能改的int。由于main里面p定义成的是一个内容是int 的数组,
: 内容是可改的,所以调用f的时候编译器就报错了。
: 俺的理解。。。

avatar
i*t
10
f(const int * p) does not need p must a const array
It just means the object that p point cannot be changed
in my understanding

【在 d*********i 的大作中提到】
: main里的int 数组不是const型
avatar
c*7
11
From "int *" to "const int *", C++ need to create a temporary const
pointer for you. Unfortunately, C++ Standard forbids creating such pointer
for non-const reference. This also explains why "const int * const &"
actually works as someone suggested.

【在 i******t 的大作中提到】
: reference to pointer问题
: void f(const int * & p)
: {
: int i =0;
: i = p[0];
: cout<< i<: system("PAUSE");
: }
: int main()
: {

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。