由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 大侠进来看看这个问题
相关主题
请教一个const pointer的问题请问如何把初始化一个const 的vector (or array) in a class?
c++ 弱问题:static const char* const 这两个const 分别是什么意思?请教一个基础C++问题
问题c++ 一问
Why should i include .cpp instead of .h[合集] 常数指针的问题
C++ Q05: pointer to constant variable请问这个C++程序有什么问题吗
const reference in copy constructor请教一个pointer的问题
why copy assignment operator returns non-const type?C++ question
C++ Q93 - Q95 (转载)why int** cannot convert to const int** ?
相关话题的讨论汇总
话题: const话题: int话题: complex话题: ival话题: pointer
进入Programming版参与讨论
1 (共1页)
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
6
感谢各位大侠,我明白了,多谢多谢
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);

1 (共1页)
进入Programming版参与讨论
相关主题
why int** cannot convert to const int** ?C++ Q05: pointer to constant variable
【讨论】为什么要用友员来实现算符重载?const reference in copy constructor
Help with a c++ const variablewhy copy assignment operator returns non-const type?
a simple C++ questionC++ Q93 - Q95 (转载)
请教一个const pointer的问题请问如何把初始化一个const 的vector (or array) in a class?
c++ 弱问题:static const char* const 这两个const 分别是什么意思?请教一个基础C++问题
问题c++ 一问
Why should i include .cpp instead of .h[合集] 常数指针的问题
相关话题的讨论汇总
话题: const话题: int话题: complex话题: ival话题: pointer