473,563 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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=Sourc eObject.propert y1 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

Dec 22 '05 #1
24 3606
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=Sourc eObject.propert y1 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
Dec 22 '05 #2

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=Sourc eObject.propert y1 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.

Dec 22 '05 #3
Hi! Sorry and where is the FAQ?
Thanks!
Jorge C.

Dec 22 '05 #4
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=Sou rceObject.prope rty1 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
Dec 22 '05 #5
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?

Dec 22 '05 #6
rdc02271 wrote:
Hi! Sorry and where is the FAQ?


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

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 22 '05 #7
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.c om

V
Dec 22 '05 #8
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
Dec 22 '05 #9
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
Dec 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
21175
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including pointers) and for objects types call their default constructor. Any others points i should know?
14
1966
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
4
7115
by: Jeff Mallett | last post by:
VC++.NET gave me Compiler Error C2621, which states, "A union member cannot have a copy constructor." Yikes, that can't be true! As I understand it, *all* class objects have copy constructors, since if they aren't explicit, one is implicitly generated. If this were true, class objects could not be members of a union, but I know they can...
10
2538
by: utab | last post by:
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,...
8
449
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make copies of classes with members with static or dynamic memory allocation. However to do this it requires the programmer to override the default copy...
74
15911
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
13
3942
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help is much appreciated. JD
7
6404
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
34
3642
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But there is a constructor that accepts the base class. This one should be used for copying. In fact i have a set of classes with a common abstract...
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.