Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy constructor: why can't I copy objects as if they were structs?

Reply
Thread Tools

Copy constructor: why can't I copy objects as if they were structs?

 
 
rdc02271
Guest
Posts: n/a
 
      12-22-2005
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.
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-22-2005
rdc02271 wrote:
> 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...
>


Have you read the FAQ yet?

V
 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-22-2005

rdc02271 wrote:
> 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...
>


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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-22-2005
Neelesh Bodas wrote:
> rdc02271 wrote:
>
>>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...
>>

>
>
> If you donot declare a copy constructor explicitly, the compiler will
> provide a default copy-constructor which will do a bitwise copy


No. The default copy-constructor does _memberwise_ copy. IOW, it will
invoke copy semantics for every member. If a member has user-defined
copy constructor, that copy constructor will be used. If some member
does not have a user-defined copy-constructor, it might still adhere to
some copy-construction rules. If members cannot be copy-constructed,
the program is ill-formed.

> 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.


That is true.

V
 
Reply With Quote
 
rdc02271
Guest
Posts: n/a
 
      12-22-2005
Hi! Sorry and where is the FAQ?
Thanks!
Jorge C.

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-22-2005
Victor Bazarov wrote:
> Neelesh Bodas wrote:


> > If you donot declare a copy constructor explicitly, the compiler will
> > provide a default copy-constructor which will do a bitwise copy

>
> No. The default copy-constructor does _memberwise_ copy. IOW, it will
> invoke copy semantics for every member. If a member has user-defined
> copy constructor, that copy constructor will be used. If some member
> does not have a user-defined copy-constructor, it might still adhere to
> some copy-construction rules. If members cannot be copy-constructed,
> the program is ill-formed.
>


Thats true. May be I wrote it in a confusing way. I said that -

> > 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*


I should have also added a comment about the non-POD members.

Also, for basic data types, isn't the memberwise copy same as the
bitwise copy? Or not necessarily?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-22-2005
rdc02271 wrote:
> Hi! Sorry and where is the FAQ?


The location of the FAQ is spelled out in the "Welcome" message
published here every week for all newcomers to read. It is also
noted many times in the archives available at groups.google.com

V
 
Reply With Quote
 
=?UTF-8?B?TWF0ZXVzeiDFgW9za290?=
Guest
Posts: n/a
 
      12-22-2005
rdc02271 wrote:
> Hi! Sorry and where is the FAQ?


http://parashift.com/c++-faq-lite/

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-22-2005
Neelesh Bodas wrote:
> [..]
> Also, for basic data types, isn't the memberwise copy same as the
> bitwise copy? Or not necessarily?


A C++ practitioner should only use the term "bitwise" when talking about
the binary operators &, |, and ^, and the unary ~.

V
 
Reply With Quote
 
=?UTF-8?B?TWF0ZXVzeiDFgW9za290?=
Guest
Posts: n/a
 
      12-22-2005
Victor Bazarov wrote:
>
> If members cannot be copy-constructed, the program is ill-formed.
>


Hm, what about members of class defined as intentionally not copyable?

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic loading problem: My app can load fancy shared objects writtenin C, but it fails if they were written in C++ Ramon F Herrera C++ 4 09-28-2009 06:31 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How to develop sites as they were in a Root George ASP .Net 2 08-26-2005 05:43 PM
executing python programs as if they were on OS path (noob) Darren Dale Python 8 08-03-2004 09:46 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments