h****s 发帖数: 29 | 1 希望用一个const 对象的地址来初始化一个引用
const int ival = 1024; // 1
const int *&pi_ref = &ival; // 2
const int *const &pi_ref = &ival; //
书上看来的,为什么第2行代码是错的,而第3行代码是对的,百思不得其解啊,多谢解释 | r*********r 发帖数: 3195 | 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. | P********e 发帖数: 2610 | 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行代码是对的,百思不得其解啊,多谢解释
| t****t 发帖数: 6806 | 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.
| S*********g 发帖数: 5298 | 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);
| h****s 发帖数: 29 | | T*******x 发帖数: 8565 | 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;
| d*******d 发帖数: 2050 | 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);
|
|