Why C++ copy constructor must use const object?

后端 未结 8 1698
夕颜
夕颜 2020-11-30 02:18

I understand that when we define a class copy constructor of the class is necessary as Rule of three states. I also notice that the argument of the copy constructor is usual

8条回答
  •  旧时难觅i
    2020-11-30 02:45

    Copy constructors should not modify the object it is copying from which is why the const is preferred on the other parameter. Both will work, but the const is preferred because it clearly states that the object passed in should not be modified by the function.

    const is for the user only. It doesn't exist for the actual executable.

提交回复
热议问题