Why C++ copy constructor must use const object?

后端 未结 8 1686
夕颜
夕颜 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:43

    In addition to the fundamental assumption that copy constructors should not modify the source instance, this article elaborates on the actual technical reason for using const:

    http://www.geeksforgeeks.org/copy-constructor-argument-const/

    Namely that and I quote:

    "... compiler created temporary objects cannot be bound to non-const references ..."

提交回复
热议问题