什么是复制构造函数的用途 [英] What is use of copy constructor

查看:112
本文介绍了什么是复制构造函数的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我不知道我是否应该问这些类型的问题,作为程序员,我有一个简单的问题,因为为什么是复制构造函数的需要

我很清楚这个概念以及如何使用复制构造函数,但是我需要一个实际的应用程序,其中必须使用复制构造函数,或者我可以重新使用将问题框定为在什么条件下我们应该使用复制构造函数。



先谢谢!!

解决方案

< blockquote>例如(据我所知) STL 容器需要复制可构造对象。

另外,如果你计划重载赋值运算符那么你也应该强制复制构造函数。


好吧,我们必须有办法告诉编译器如何将我们类的对象复制到另一个对象中。当我们将对象作为参数传递给函数和其他几种情况时,会发生这种复制操作。在这种情况下,编译器使用复制构造函数。如果你没有声明一个,它会根据它的最佳知识创建它的默认版本,简单地说就是按成员复制对象成员。



对于某些人来说类这个默认的复制构造函数不是我们想要做的。例如,假设您的类包含指向另一个对象的指针并且拥有该对象。那个对象。在这种情况下,您希望能够指定指针值究竟应该发生什么,即我们是否希望创建另一个现在由副本拥有的对象?或者我们可能需要增加引用计数...



你现在明白了,为什么我们必须指定如何复制对象?


在许多情况下都需要复制构造函数,即使您没有明确地调用它。一些例子是:



1.按值将对象作为函数参数传递

2.按值返回函数中的对象

3.创建临时副本,e。 G。当覆盖postifx运算符++或 -



而你可以选择解决第三种情况,并且可能(实际上应该!)传递一个const引用而不是第一种情况下的值,第二种情况不容易避免。



请注意,通常但不总是可以使用赋值运算符代替复制构造函数。这两者密切相关。



http:// www。 cplusplus.com/articles/y8hv0pDG/ [ ^ ]了解更多信息。


Hi,

I don't know if i should be asking these type of question, by as a programmer i have simple question as why is the need of copy constructor.
I know the concept well and how to use the copy constructor, but i need a practical application where copy constructor is mandatory to be used, or i can re frame the question as In what condition we should be using copy constructor.

Thanks in Advance !!

解决方案

For instance (as far as I know) STL containers requires copy contructable objects.
Also, if you plan to overload the assignment operator then you should also impement the copy constructor.


Well, somehow we must have a way to tell the compiler how it can copy an object of our class into another. Such copy operations happen when we pass an object as a parameter to a function and several other situations. In such cases the compiler uses the copy constructor. And if you don't declare one, it creates a default version of it to its best knowledge, which in simple words copies the object member by member.

For some classes this default copy constructor does not what we want to do. For example, imagine your class contains a pointer to another object and "owns" that object. In such cases you want to be able to specify what exactly should happen with the pointer value, i.e. do we want another object be created which is now owned by the copy? Or we might need to increment a reference count ...

Do you understand now, why it is necessary that we specify how objects should be copied?


The copy constructor is needed in many situations, even if you do not call it explicitely. Some examples are:

1. Pass an object as a function argument by value
2. Return an object from a function by value
3. Create a temporary copy, e. g. when overriding the postifx operators ++ or --

while you can choose to work around the third case, and may (in fact, should!) pass a const reference instead of a value in the first case, the second case cannot be easily avoided.

Note that often, but not always, the assignment operator can be used instead of the copy constructor. these two are closely related.

See http://www.cplusplus.com/articles/y8hv0pDG/[^] for more info.


这篇关于什么是复制构造函数的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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