复制构造函数变得方便吗? [英] When copy constructors become handy?

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

问题描述

亲爱的,


因此传递和返回类对象是将复制构造函数的

定义包含在类定义中的时间。但是如果我们不按价值调用或按价值返回,我们就不需要使用

复制构造函数了。所以根据上面的推理,我可以避免按值调用

并按类值返回值,这会绕过

问题,或者在我看来就是这样。 />

当复制构造函数是C ++类实现中不可或缺的工具时,有没有人能给我一些简单的例子?


Thx

Dear all,

So passing and returning a class object is the time when to include the
definition of the copy constructor into the class definition. But if we
don''t call by value or return by value, we do not need to use the
copy-constructor. So depending on the above reasoning I can avoid call
by value and return by value for class objects, this bypasses the
problem or it seems to me like that.

Could any one give me some simple examples when copy constructors are
indispensible tools in C++ class implementations?

Thx

推荐答案

utab写道:
亲爱的,

返回类对象是将复制构造函数的定义包含在类定义中的时间。


不一定。有一个默认的复制ctor自动提供。

如果默认不满意,你只需要自己编写。

但是如果我们不这样做按值调用或按值返回,我们不需要使用
复制构造函数。因此,根据上面的推理,我可以避免按值调用
并按类值返回值,这会绕过问题,或者在我看来就是这样。


函数参数和返回值不是复制

对象的唯一情况。有时你只需要复制一份:


A a1;

A a2 = a1; //复制ctor叫


此外,你可能会发现永远不会打电话或按价值返回

令人难以置信的繁重。你对拷贝ctors有什么看法?

当拷贝构造器是C ++类实现中不可或缺的工具时,有没有人能给我一些简单的例子呢?

Thx
Dear all,

So passing and returning a class object is the time when to include the
definition of the copy constructor into the class definition.
Not necessarily. There is a default copy ctor provided automatically.
You only need to write your own if the default is not satisfactory.
But if we
don''t call by value or return by value, we do not need to use the
copy-constructor. So depending on the above reasoning I can avoid call
by value and return by value for class objects, this bypasses the
problem or it seems to me like that.
Function arguments and return values aren''t the only situations where
objects are copied. Sometimes you just need to make a copy:

A a1;
A a2 = a1; // copy ctor called

Besides, you may find that never calling or returning by value is
incredibly onerous. What do you have against copy ctors?

Could any one give me some simple examples when copy constructors are
indispensible tools in C++ class implementations?

Thx





utab写道:

utab wrote:
亲爱的,
因此,传递和返回类对象是将复制构造函数的定义包含在类定义中的时间。


按值传递并按值返回是两种方式为类类型的对象调用复制

构造。但仅仅因为使用了副本

构造函数并不意味着你需要自己定义它。如果你没有定义它,那么编译器会为你做这件事。编译器

生成的复制构造函数将执行所有数据的成员副本

成员,并将使用其复制构造函数复制基类对象。

除非这不足以满足您的需求,即使您复制了对象,也不需要定义复制构造函数。

但是如果我们不这样做的话不是按值调用或按值返回,我们不需要使用
复制构造函数。


不正确。


std :: string s1(" text");

std :: string s2(s1);


s2是使用std :: string类的复制构造函数构造的,

但是没有通过值传递或返回任何内容。

因此,根据上述推理,我可以避免按值调用
并按类对象返回值,这会绕过
问题,或者在我看来就是这样。


有什么问题?

当复制构造函数是C ++类实现中不可或缺的工具时,任何人都可以给我一些简单的例子吗?
Dear all,

So passing and returning a class object is the time when to include the
definition of the copy constructor into the class definition.
Passing by value and returning by value are two ways of invoking copy
construction for objects of class type. But just because the copy
constructor is used doesn''t mean you need to define it yourself. If you
don''t define it, the compiler will do it for you. The compiler
generated copy constructor will perform a memberwise copy of all data
members and will copy base class objects using their copy constructors.
Unless that is not sufficient for your needs, you do not need to define
the copy constructor, even if you do copy objects.
But if we
don''t call by value or return by value, we do not need to use the
copy-constructor.
Not true.

std::string s1("text");
std::string s2(s1);

s2 is constructed using the copy constructor of the std::string class,
but nothing is passed or returned by value.
So depending on the above reasoning I can avoid call
by value and return by value for class objects, this bypasses the
problem or it seems to me like that.
What problem?
Could any one give me some simple examples when copy constructors are
indispensible tools in C++ class implementations?




您应该为编译器生成的复制构造函数不足的任何类编写复制构造函数。通常这意味着拥有资源的
类,例如由光头指针引用的记忆。

谷歌的三个规则太。如果你需要编写自己的副本

构造函数,你可能需要编写自己的赋值运算符

和析构函数。


请注意,决定是否编写自己的复制构造函数或

依赖于编译器生成的一个*没有*与你是否* b $ b认为你的代码有关'即将写的将复制对象。即使你不打算立即复制任何对象,你仍然应该编写

复制构造函数(或禁用复制 - 见下文)如果编译器

生成的复制构造函数是不够的。否则,有一天你或者其他人会来,写一些新的代码进行复制和

无意中引入了错误,因为使用了错误的拷贝构造函数。


您还有其他选择。如果要禁用

类的复制,请将复制构造函数声明为private。宣布赋值

运算符私有,因为禁用

复制但允许赋值当然没有意义。


class foo

{

私人:

foo(const foo&);

foo& operator =(const foo&);

};


尝试复制或分配foo对象将导致编译

错误。声明自己的复制构造函数(即使它是私有的

并且你没有提供定义)会抑制编译器

生成一个。


Gavin Deane



You should write a copy constructor for any class where the compiler
generated copy constructor is not sufficient. Generally this means
classes that own resources, e.g. memory referred to by a bald pointer.
Google for the "rule of three" too. If you need to write your own copy
constructor, you probably need to write your own assignment operator
and destructor as well.

Notice that the decision whether to write your own copy constructor or
rely on the compiler generated one has *nothing* to do with whether you
think the code you''re about to write will copy objects. Even if you
don''t plan to copy any objects immediately, you should still write the
copy constructor (or disable copying - see below) if the compiler
generated copy constructor is not sufficient. Otherwise, one day you or
someone else will come along, write some new code that does copying and
unwittingly introduce bugs because the wrong copy constructor is used.

You have another option too. If you want to disable copying for your
class, declare the copy constructor private. Declare the assignment
operator private too because it certainly makes no sense to disable
copying but allow assignment.

class foo
{
private:
foo(const foo&);
foo& operator=(const foo&);
};

An attempt to copy or assign foo objects will result in a compile
error. Declaring your own copy constructor (even though it is private
and you have not provided a definition) suppresses the compiler
generated one.

Gavin Deane



A a1;
A a2 = a1; //复制ctor叫
A a1;
A a2 = a1; // copy ctor called



这个默认的复制构造函数可以解决这个问题吗?


for this the default copy constructor does the trick or ??


这篇关于复制构造函数变得方便吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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