由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++: Q75 copy constructor 问什么用 const reference?
相关主题
C++ Q83: 这个const_cast什么意思?c++ 数组问题
问一道无聊的bloomberg电面题问一个C++问题:default parameter and overriding/inheritanc
C++ templateC++ online Test 又一题
C++ Q61: 如何对const data member做assignment?发个电话面经
请教一个C++问题还是基础题
询问Bloomberg online skill assessment问一个constructor的问题
问个C++ ctor的问题C++ Question
再帖一遍Amazon Onsite的题C++ 一题
相关话题的讨论汇总
话题: const话题: reference话题: copy话题: c++
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
大家都知道为什么用reference。但为什么要 const reference?
试了一下,如果用const reference,那么不是 const 的输入也可以。
但如果不用 const reference, 那么 const 的输入 object 就不可以。
但是这个问题的标准答案是什么?还望大牛指点。
s*********t
发帖数: 1663
2
const表示该函数承诺不会修改该object
const引用不能修改object,所以不能传进非const的函数里

【在 c**********e 的大作中提到】
: 大家都知道为什么用reference。但为什么要 const reference?
: 试了一下,如果用const reference,那么不是 const 的输入也可以。
: 但如果不用 const reference, 那么 const 的输入 object 就不可以。
: 但是这个问题的标准答案是什么?还望大牛指点。

m*********2
发帖数: 701
3
data integrity reason
copy constructor不应该改reference object.

【在 c**********e 的大作中提到】
: 大家都知道为什么用reference。但为什么要 const reference?
: 试了一下,如果用const reference,那么不是 const 的输入也可以。
: 但如果不用 const reference, 那么 const 的输入 object 就不可以。
: 但是这个问题的标准答案是什么?还望大牛指点。

c**y
发帖数: 172
4
By using "const", it indicates that the copy constructor would not change
anything of the parameter being passed.
A non-const reference parameter is automatically cast into a const reference
by the compiler.
However, if the copy constructor is defined with a non-const reference
parameter, then passing a const reference can't go through because the
compiler can't cast the const reference automatically to a non-const
reference. In C++, dynamic_cast is needed to get rid of consistness of the
object.

【在 c**********e 的大作中提到】
: 大家都知道为什么用reference。但为什么要 const reference?
: 试了一下,如果用const reference,那么不是 const 的输入也可以。
: 但如果不用 const reference, 那么 const 的输入 object 就不可以。
: 但是这个问题的标准答案是什么?还望大牛指点。

m*********2
发帖数: 701
5
really?
1) non-const will automatically convert to const when passing?
2) using dynamic_cast<> instead of const_cast<>?

change
reference
the

【在 c**y 的大作中提到】
: By using "const", it indicates that the copy constructor would not change
: anything of the parameter being passed.
: A non-const reference parameter is automatically cast into a const reference
: by the compiler.
: However, if the copy constructor is defined with a non-const reference
: parameter, then passing a const reference can't go through because the
: compiler can't cast the const reference automatically to a non-const
: reference. In C++, dynamic_cast is needed to get rid of consistness of the
: object.

c**y
发帖数: 172
6
1) If the parameter is defined as a const, but a non-const is passed, then
the non-const will be cast into a const inside the copy constructor, which
is done by compiler.
2) You're right, it should be const_cast. Thanks for correcting.

【在 m*********2 的大作中提到】
: really?
: 1) non-const will automatically convert to const when passing?
: 2) using dynamic_cast<> instead of const_cast<>?
:
: change
: reference
: the

c*******n
发帖数: 63
7
使用reference是避免递归调用copy constructor
A a;
A b = a;
如果不用reference,调用copy constructor的时候需要先copy a作为copy
constructor的参数,这就发生无穷递归了
1 (共1页)
进入JobHunting版参与讨论
相关主题
C++ 一题请教一个C++问题
请问这个C++问题对吗?询问Bloomberg online skill assessment
C++ Q47: protected constructor (C39)问个C++ ctor的问题
copy constructor 的问题再帖一遍Amazon Onsite的题
C++ Q83: 这个const_cast什么意思?c++ 数组问题
问一道无聊的bloomberg电面题问一个C++问题:default parameter and overriding/inheritanc
C++ templateC++ online Test 又一题
C++ Q61: 如何对const data member做assignment?发个电话面经
相关话题的讨论汇总
话题: const话题: reference话题: copy话题: c++