为什么在Copy构造函数的情况下使用引用技术调用 [英] Why Call by reference technique is used in case of Copy constructor

查看:89
本文介绍了为什么在Copy构造函数的情况下使用引用技术调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们通过值方法调用使用复制构造函数时会发生什么?

如果出现此错误有什么错误?

解决方案

要按值将对象传递给函数,需要将该对象复制到参数对象。



因此复制构造函数将被调用...



...尚未定义!



由于编译器没有通灵能力来解决循环依赖关系,你得到一个错误。



如果你想让你的代码安全,那么使用const引用作为你的参数。



Iain。


如果在复制构造函数中使用按值调用方法,那么对象进程的创建将进入无限循环。


< blockquote>




如果你通过值复制构造函数,它将 call 本身, 将实际参数复制到形式参数,它将调用无限链 调用 复制constucter,此过程将 ,直到系统用完 内存。


what happen when we use copy contructor by call by value method?
if any error why this error is occured?

解决方案

To pass an object by value to a function, then that object needs to be copied to the parameter object.

So the copy constructor would be called...

...which is not yet defined!

As the compiler does not have psychic powers to solve circular dependencies, you get an error.

If you want to make your code safe, then use a const reference as your parameter.

Iain.


If You use call by value method in copy constructor then creation of object process will go in infinity loop.


Hi,


If u passed by value to copy constructer,it will call itself,to copy the actual parameter to formal parameter,it will call a endless chain of call to copy constucter,this process would go on untill the system runs out of memory.


这篇关于为什么在Copy构造函数的情况下使用引用技术调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆