复制构造函数:为什么我不能复制对象就像它们是结构一样? [英] Copy constructor: why can't I copy objects as if they were structs?

查看:83
本文介绍了复制构造函数:为什么我不能复制对象就像它们是结构一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

这太疯狂了吗?

复制构造函数:为什么我不能复制对象就好像它们是结构一样?

我有一组简单的对象(没有字符串属性,只有整数,

双倍),我必须复制相同的对象数百万次。

所以相反在复制构造函数中写入

property1 = SourceObject.property1我不能使用内存复制功能

这样做更快吗?

是这太傻了?

顺便说一句,我是C ++新手!但是,因为......不要轻易放弃...

;)


再见!感谢您的帮助和关注。

Jorge C.
rd ****** @ yahoo.com

Hello!
Is this too crazy or not?
Copy constructor: why can''t I copy objects as if they were structs?
I have a set of simple objects (no string properties, just integers,
doubles) and I have to copy the same object millions of times.
So instead of writing in the copy constructor
property1=SourceObject.property1 can''t I use memory copy functions to
do this faster?
Is this too stupid?
By the way, I''m a C++ newbie! But don''t go easy on me just because...
;)

Bye! Thanks for your help and attention.
Jorge C.
rd******@yahoo.com

推荐答案

rdc02271写道:
rdc02271 wrote:
这太疯狂了吗?
复制构造函数:为什么我不能复制对象就好像它们是结构一样?


这是什么意思?老实说,我对这样的问题感到困惑。 为什么

我不能用铅笔写?你如何回答这样的问题?

我有一组简单的对象(没有字符串属性,只有整数,
双倍),我必须复制相同的对象数百万次。


OK。

所以不要在复制构造函数中写入
property1 = SourceObject.property1我不能使用内存复制函数来
做得更快?


当然。如果您的类是POD类,则可以使用memcpy。一般来说,如果你不编写你的拷贝构造函数,那么
,编译器提供一,

它会为所有成员调用复制构造语义。

这太愚蠢了吗?


我也不知道如何回答这个问题。

顺便说一句,我是C ++新手!但是,因为......不要轻易放弃我。
;)
Is this too crazy or not?
Copy constructor: why can''t I copy objects as if they were structs?
What does it mean? I honestly am baffled by questions like this. "Why
can''t I use a pencil to write"? How do you answer such a question?
I have a set of simple objects (no string properties, just integers,
doubles) and I have to copy the same object millions of times.
OK.
So instead of writing in the copy constructor
property1=SourceObject.property1 can''t I use memory copy functions to
do this faster?
Sure. If your class is a POD class, you can use memcpy. In general,
if you don''t write your copy constructor, the compiler "provides" one,
and it invokes copy constructing semantics for all members.
Is this too stupid?
I don''t know how to answer this either.
By the way, I''m a C++ newbie! But don''t go easy on me just because...
;)




您是否已阅读常见问题解答?

V



Have you read the FAQ yet?

V




rdc02271写道:

rdc02271 wrote:
你好!
这太疯了是不是?
复制构造函数:为什么我不能复制对象就像它们是结构一样?
我有一组简单的对象(没有字符串属性,只有整数,
双倍)和我必须复制相同的对象数百万次。
因此,而不是在复制构造函数中写入
property1 = SourceObject.property1我不能使用内存复制功能来更快地执行此操作吗?
这太愚蠢了吗?顺便说一句,我是C ++的新手!但是不要因为......而对我不要轻松......
;)
Hello!
Is this too crazy or not?
Copy constructor: why can''t I copy objects as if they were structs?
I have a set of simple objects (no string properties, just integers,
doubles) and I have to copy the same object millions of times.
So instead of writing in the copy constructor
property1=SourceObject.property1 can''t I use memory copy functions to
do this faster?
Is this too stupid?
By the way, I''m a C++ newbie! But don''t go easy on me just because...
;)




如果你没有明确声明一个拷贝构造函数,那么编译器会

提供了一个默认的复制构造函数,它将对基本类型的

数据成员进行按位复制。因此,如果你的类只包含整数

和double,你可以完全跳过完全写CC -

编译生成的构造函数将为你做必要的工作。 />

HTH。



If you donot declare a copy constructor explicitly, the compiler will
provide a default copy-constructor which will do a bitwise copy of the
data members of basic types. So if your class contains only integers
and doubles, you may very well skip writing the CC completely - the
compiler generated constructor will do the necessary work for you.

HTH.


嗨!对不起,FAQ在哪里?

谢谢!

Jorge C.

Hi! Sorry and where is the FAQ?
Thanks!
Jorge C.


这篇关于复制构造函数:为什么我不能复制对象就像它们是结构一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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