473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Dec 22 '05 #1
24 3585
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
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=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.

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

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

Victor Bazarov wrote:
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 ~.


Ok. That is a valuable guideline. Thanks.

Dec 22 '05 #11
Mateusz Łoskot wrote:
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?


I don't understand the question, given the context.

V
Dec 22 '05 #12
Victor Bazarov wrote:
Mateusz Łoskot wrote:
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?

I don't understand the question, given the context.

V


I think he's asking about member objects that are designed not to be
copyable (e.g. private or undefined copy constructor).

(Answer: The code won't let you copy the object. It is a compile-time
error.)
Dec 22 '05 #13
Victor Bazarov wrote:
Mateusz Łoskot wrote:
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?


I don't understand the question, given the context.


I'll try to be more precise.
You said:

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

and I asked if the last rule (If members cannot be copy-constructed, the
program is ill-formed.) only applies to the situation you described
above: if class of which type is a member does have user-defined copy
constructor or generated by compiler then such member is considered as
copy-constructable but if it still can not be copy-constructed then the
program is ill-formed.

Or may be your rule of "ill-formed program" appllies to all classes with
data members, even if you have private copy-constructor.
Hm, I'm still not sure if my explanation is clear enough.
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 22 '05 #14
Mateusz Łoskot wrote:
Victor Bazarov wrote:
Mateusz Łoskot wrote:
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?

I don't understand the question, given the context.


I'll try to be more precise.
You said:

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

and I asked if the last rule (If members cannot be copy-constructed, the
program is ill-formed.) only applies to the situation you described
above: if class of which type is a member does have user-defined copy
constructor or generated by compiler then such member is considered as
copy-constructable but if it still can not be copy-constructed then the
program is ill-formed.


You use sentences that are too long for my feeble brain.

class A { A(A&); }; // private copy c-tor
class B { A a; }; // not copy-constructible - 'a' cannot be copied
class C { B b; }; // no user-defined copy c-tor
void foo(C& c) {
C cc(c); // ill-formed
}

For the class C the compiler cannot create a copy-c-tor because 'b' does
not have "default" copy semantics because 'A' doesn't.. A program that
needs copy-construction of a 'B' or a 'C' is ill-formed.

class A { A(A&); }; // private copy c-tor
class B { A& a; }; // no user-defined copy c-tor, but 'a' can be copied
class C { B b; }; // no user-defined copy c-tor, but 'b' can be copied
void foo(C& c) {
C cc(c); // perfectly OK
}

'B' is perfectly copy-constructible, and so is 'C'. There is the issue
of constructing an object of type 'B' to begin with, but it's not what we
are talking about here.
Or may be your rule of "ill-formed program" appllies to all classes with
data members, even if you have private copy-constructor.
Again, I don't understand even this sentence. *I* don't have private
copy-constructor. Only user-defined types can have copy-constructors.
Hm, I'm still not sure if my explanation is clear enough.


I didn't get it.

V
Dec 22 '05 #15
> Victor Bazarov wrote:
A C++ practitioner should only use the term "bitwise" when talking about
the binary operators &, |, and ^, and the unary ~.

V


How about bitset::operator[]? :>

- J.
Dec 22 '05 #16
Jacek Dziedzic wrote:
> Victor Bazarov wrote:

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

V

How about bitset::operator[]? :>


That overloaded operator is called "indexing". I would expect a C++
programmer to be familiar with it under that name.

V
Dec 22 '05 #17
Victor Bazarov wrote:
rdc02271 wrote:
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.


Adding a copy-constructor renders the class a non-POD.

Dec 22 '05 #18
Old Wolf wrote:
Victor Bazarov wrote:

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

Adding a copy-constructor renders the class a non-POD.


I was talking of the members.
Dec 22 '05 #19
Neelesh Bodas wrote:
...
Also, for basic data types, isn't the memberwise copy same as the
bitwise copy? Or not necessarily?
...


It depends on what exactly you understand under "bitwise copy". For basic data
types "memberwise copy" means copying all bits involved in value-representation
of given type, but it doesn't guarantee that non-value-representing bits will be
copied. This essentially means that the behavior might be different from what
'memcpy' does, since the latter always copies everything.

In other words, if one object is copied into another object using the implicitly
defined (compiler-provided) copy constructor, it is still not guaranteed that a
'memcmp' applied to these objects afterwards will report a match. Even if we are
dealing with POD objects.

--
Best regards,
Andrey Tarasevich
Dec 23 '05 #20
Victor Bazarov wrote:
Jacek Dziedzic wrote:
> Victor Bazarov wrote:

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

V


How about bitset::operator[]? :>

That overloaded operator is called "indexing". I would expect a C++
programmer to be familiar with it under that name.


I know, but isn't the manner in which it indexes bit-wise?

- J.
Dec 23 '05 #21
Jacek Dziedzic wrote:
Victor Bazarov wrote:
Jacek Dziedzic wrote:
> Victor Bazarov wrote:

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

V


How about bitset::operator[]? :>


That overloaded operator is called "indexing". I would expect a C++
programmer to be familiar with it under that name.

I know, but isn't the manner in which it indexes bit-wise?


Well, as opposed to what? Indexing them logically? Indexing them whole?
Dec 23 '05 #22
Victor Bazarov wrote:
You use sentences that are too long for my feeble brain.

class A { A(A&); }; // private copy c-tor
class B { A a; }; // not copy-constructible - 'a' cannot be copied
class C { B b; }; // no user-defined copy c-tor
void foo(C& c) {
C cc(c); // ill-formed
}

For the class C the compiler cannot create a copy-c-tor because 'b' does
not have "default" copy semantics because 'A' doesn't.. A program that
needs copy-construction of a 'B' or a 'C' is ill-formed.

class A { A(A&); }; // private copy c-tor
class B { A& a; }; // no user-defined copy c-tor, but 'a' can be copied
class C { B b; }; // no user-defined copy c-tor, but 'b' can be copied
void foo(C& c) {
C cc(c); // perfectly OK
}

'B' is perfectly copy-constructible, and so is 'C'. There is the issue
of constructing an object of type 'B' to begin with, but it's not what we
are talking about here.


Thank you for your great patience. You gave me explenation I tried to
ask for. Simply, I couldn't express my question as simple as I should.
Why? I'm not sure, may be I didn't understand the problem as I should
firt. Now, I know what you had in mind in your previous post.
Thank you
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 23 '05 #23
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 cannot afford a copy constructor, one will be appointed for
you before questioning.

The compiler generates a copy constructor if the user doesn't define
one. If there are no subobjects that themselves have copy constructors
it almost certainly does a very fast block copy of the object.


Dec 23 '05 #24
Victor Bazarov wrote:
Jacek Dziedzic wrote:
Victor Bazarov wrote:
Jacek Dziedzic wrote:

> Victor Bazarov wrote:

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

How about bitset::operator[]? :>


That overloaded operator is called "indexing". I would expect a C++
programmer to be familiar with it under that name.


I know, but isn't the manner in which it indexes bit-wise?

Well, as opposed to what? Indexing them logically? Indexing them whole?


As opposed to nothing, actually. What I meant to say was that
a "C++ practitioner" could easily use the word "bitwise" in
contexts other than involving "bitwise operators" that you
have mentioned.

My point was that a statement like
"the bitset::operator[] overloaded operator allows for bitwise
access to the bitset" would be pretty legal.

But in fact I won't argue.

eot,
- J.
Dec 27 '05 #25

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

Similar topics

15
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...
14
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
4
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,...
10
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, ...
8
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...
74
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...
13
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...
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
34
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.