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
|