gpt4 book ai didi

c++ - 为什么复制构造函数参数是 const?

转载 作者:IT老高 更新时间:2023-10-28 12:46:29 25 4
gpt4 key购买 nike

 Vector(const Vector& other) // Copy constructor 
{
x = other.x;
y = other.y;

为什么参数是 const?

最佳答案

你得到的答案提到确保 ctor 不能改变被复制的内容——他们是对的,把 const 放在那里确实有这种效果。

然而,更重要的是临时对象不能绑定(bind)到非常量引用。复制 ctor 必须引用 const 对象才能复制临时对象。

关于c++ - 为什么复制构造函数参数是 const?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1602058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com