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

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

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

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 17:34

    It is very essential to pass objects as reference. If an object is passed as value to the Copy Constructor then its copy constructor would call itself, to copy the actual parameter to the formal parameter. Thus an endless chain of call to the copy constructor will be initiated. This process would go on untill the system run out of memory.

    Hence, in a copy constructor, the parameter should always be passed as reference.

提交回复
热议问题