Why C++ copy constructor must use const object?

后端 未结 8 1705
夕颜
夕颜 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条回答
  •  孤街浪徒
    2020-11-30 02:29

    As several other answers point out, a copy constructor that modified its argument would be an unpleasant surprise. That is not, however, the only problem. Copy constructors are sometimes used with arguments that are temporaries. (Example: return from function.) And non-const references to temporaries don't fly, as explained elsewhere on SO.

提交回复
热议问题