由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - const reference in copy constructor
相关主题
C++ question前几天有人问rvalue reference的
array如何get set?请教一个关于std::function的问题
C++: define a reference always reference the same objectC++问题,confusing...
How to initialize object in constructor?a simple C++ question
大侠进来看看这个问题C++ operator = overloading用copy & swap有啥优点
请教一个boost::bind的问题copy constructor问题。
请教cosnt的使用被reference搞晕了
const object[c++] reference 真得不能bound to a second object 么?
相关话题的讨论汇总
话题: const话题: reference话题: rhs话题: object
进入Programming版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
copy constructor 为什么用const reference. 我解释关于为什么用reference是 对的
,对于为什么用const,我的解释是 prevent the constructor from modifying the
object being copied. tnnd他说不对
A::A(const A& rhs) {...}
What does the const mean?
Somebody claims "The question is what const means here? I think it means
that one is prohibited to change the constant reference passed in. In
contrast, I do not think it means that one must pass in a reference of a
constant object to call the copy constructor."
Is he right or wrong? Thanks.
f*******n
发帖数: 12623
2
He is wrong.
References are always constant. You cannot make a reference point to
something else after it is defined.
Even for pointers, the syntax "const A *rhs" or "A const *rhs" means pointer
to constant object. Only "A *const rhs" means "constant pointer".
r****t
发帖数: 10904
3
point here is to allow copy construct from a const object

【在 c**********e 的大作中提到】
: copy constructor 为什么用const reference. 我解释关于为什么用reference是 对的
: ,对于为什么用const,我的解释是 prevent the constructor from modifying the
: object being copied. tnnd他说不对
: A::A(const A& rhs) {...}
: What does the const mean?
: Somebody claims "The question is what const means here? I think it means
: that one is prohibited to change the constant reference passed in. In
: contrast, I do not think it means that one must pass in a reference of a
: constant object to call the copy constructor."
: Is he right or wrong? Thanks.

t****t
发帖数: 6806
4
no, the point here is to allow temporary object to bind. temporary object
only binds to const reference. for example:
class A {
public:
A(A&);
};
A operator+(const A&, const A&);
A a1;
A a2(a1); // OK
A a3(A()); // wrong
A a4(a1+a2); // wrong
class B {
public:
B(const B&);
};
B operator+(const B&, const B&);
B b1;
B b2(b1); // OK
B b3(B()); // OK
B b4(b1+b2); // OK

【在 r****t 的大作中提到】
: point here is to allow copy construct from a const object
T******r
发帖数: 257
5
more generally, to allow const object to bind.
for your class A,
const A a4;
A a5(a4); // wrong

【在 t****t 的大作中提到】
: no, the point here is to allow temporary object to bind. temporary object
: only binds to const reference. for example:
: class A {
: public:
: A(A&);
: };
: A operator+(const A&, const A&);
: A a1;
: A a2(a1); // OK
: A a3(A()); // wrong

t****t
发帖数: 6806
6
这个是基本的, suppose每个人都知道的. 上面那个, 据说不是每个人都知道...

【在 T******r 的大作中提到】
: more generally, to allow const object to bind.
: for your class A,
: const A a4;
: A a5(a4); // wrong

r****t
发帖数: 10904
7
good one.

【在 t****t 的大作中提到】
: no, the point here is to allow temporary object to bind. temporary object
: only binds to const reference. for example:
: class A {
: public:
: A(A&);
: };
: A operator+(const A&, const A&);
: A a1;
: A a2(a1); // OK
: A a3(A()); // wrong

b***i
发帖数: 3043
8
const A& rhs 等价于 A const & rhs 就是rhs 是不改变A值的引用
我觉得,你说得有道理。第二点,就是thrust提到的,temporary 能bind 到const
reference,而不能非const。他不应该说你错,而应该是不完全。新标准可能对此有变
动,我得研究一下。

【在 c**********e 的大作中提到】
: copy constructor 为什么用const reference. 我解释关于为什么用reference是 对的
: ,对于为什么用const,我的解释是 prevent the constructor from modifying the
: object being copied. tnnd他说不对
: A::A(const A& rhs) {...}
: What does the const mean?
: Somebody claims "The question is what const means here? I think it means
: that one is prohibited to change the constant reference passed in. In
: contrast, I do not think it means that one must pass in a reference of a
: constant object to call the copy constructor."
: Is he right or wrong? Thanks.

c**********e
发帖数: 2007
9
老大的功力令人赞叹。这个问题困扰我两年多了。一直没有找到令人信服的答案。现在
终于找到了。非常感谢。

【在 t****t 的大作中提到】
: no, the point here is to allow temporary object to bind. temporary object
: only binds to const reference. for example:
: class A {
: public:
: A(A&);
: };
: A operator+(const A&, const A&);
: A a1;
: A a2(a1); // OK
: A a3(A()); // wrong

1 (共1页)
进入Programming版参与讨论
相关主题
[c++] reference 真得不能bound to a second object 么?大侠进来看看这个问题
请问个c++ primer里面的小白问题.请教一个boost::bind的问题
菜鸟请教smart pointer请教cosnt的使用
why int** cannot convert to const int** ?const object
C++ question前几天有人问rvalue reference的
array如何get set?请教一个关于std::function的问题
C++: define a reference always reference the same objectC++问题,confusing...
How to initialize object in constructor?a simple C++ question
相关话题的讨论汇总
话题: const话题: reference话题: rhs话题: object