Why should the copy constructor accept its parameter by reference in C++?

前端 未结 8 1312
离开以前
离开以前 2020-11-22 17:00

Why must a copy constructor\'s parameter be passed by reference?

8条回答
  •  没有蜡笔的小新
    2020-11-22 17:34

    Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on...

    (You would have infinite recursion because "to make a copy, you need to make a copy".)

提交回复
热议问题