avatar
大侠进来看看这个问题# Programming - 葵花宝典
h*s
1
希望用一个const 对象的地址来初始化一个引用
const int ival = 1024; // 1
const int *&pi_ref = &ival; // 2
const int *const &pi_ref = &ival; //
书上看来的,为什么第2行代码是错的,而第3行代码是对的,百思不得其解啊,多谢解释
avatar
r*r
2
1. ival doesn't always exist in the memory. the compiler can "optimize it
away" since it's a constant.
2. rr_ref is a pointer, it has to point to some memory address. this is
wrong, because ival may not even exist in the memory
3. rr_ref is a constant pointer ( by the second const), it can also be "
optimized away". theoretically it's correct, but I cann't imagine how it can
be useful.
avatar
P*e
3
ok,it takes some time to make it clear
const int i = 0;
1.use const before * are the same
const int *p =&i; <==> int const *p = &i;
(pointer to a const int, can't change value via p)
2.use const after * is different
int* const p =&i;
(const pointer, can't let p point to other int, only i)
3. const int *pi = &i;
const int *& p = pi;
(*& means reference to a non-const pointer to a const int, you can change
address via p, but not value of the integer)
4. const int * const &p = &i;
(*const& means

【在 h****s 的大作中提到】
: 希望用一个const 对象的地址来初始化一个引用
: const int ival = 1024; // 1
: const int *&pi_ref = &ival; // 2
: const int *const &pi_ref = &ival; //
: 书上看来的,为什么第2行代码是错的,而第3行代码是对的,百思不得其解啊,多谢解释

avatar
t*t
4
其实就是记住, temp object只能bind to const reference就行了
比如说
const int& j=1; //valid
int& j=2; //invalid
虽然这样显式的写法比较少, 但是temp object bind to const reference在函数调用
时还是非常常见的.
比如说
class complex {
public:
complex(double);
};
complex operator+(const complex&, const complex&);
complex a=1;
complex c=a+2;
最后那一行, 实际上做的是
complex c=operator+(a, complex(2));
complex(2)是个临时对象, 必须bind to const reference. 如果op+改成
complex operator+(complex&, complex&);
就不能这么写了.

can

【在 r*********r 的大作中提到】
: 1. ival doesn't always exist in the memory. the compiler can "optimize it
: away" since it's a constant.
: 2. rr_ref is a pointer, it has to point to some memory address. this is
: wrong, because ival may not even exist in the memory
: 3. rr_ref is a constant pointer ( by the second const), it can also be "
: optimized away". theoretically it's correct, but I cann't imagine how it can
: be useful.

avatar
S*g
5
前几天看到一个blog把这种const叫做most important const

【在 t****t 的大作中提到】
: 其实就是记住, temp object只能bind to const reference就行了
: 比如说
: const int& j=1; //valid
: int& j=2; //invalid
: 虽然这样显式的写法比较少, 但是temp object bind to const reference在函数调用
: 时还是非常常见的.
: 比如说
: class complex {
: public:
: complex(double);

avatar
h*s
6
感谢各位大侠,我明白了,多谢多谢
avatar
T*x
7
reference我一直没搞明白。

【在 P********e 的大作中提到】
: ok,it takes some time to make it clear
: const int i = 0;
: 1.use const before * are the same
: const int *p =&i; <==> int const *p = &i;
: (pointer to a const int, can't change value via p)
: 2.use const after * is different
: int* const p =&i;
: (const pointer, can't let p point to other int, only i)
: 3. const int *pi = &i;
: const int *& p = pi;

avatar
d*d
8
嗯, 这个是正解

【在 t****t 的大作中提到】
: 其实就是记住, temp object只能bind to const reference就行了
: 比如说
: const int& j=1; //valid
: int& j=2; //invalid
: 虽然这样显式的写法比较少, 但是temp object bind to const reference在函数调用
: 时还是非常常见的.
: 比如说
: class complex {
: public:
: complex(double);

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