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
24 3621
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.
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
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.)
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
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
> 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::operato r[]? :>
- J.
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::operato r[]? :>
That overloaded operator is called "indexing". I would expect a C++
programmer to be familiar with it under that name.
V
Victor Bazarov wrote: rdc02271 wrote: 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.
Adding a copy-constructor renders the class a non-POD.
Old Wolf wrote: Victor Bazarov wrote:
rdc02271 wrote:
So instead of writing in the copy constructor property1=So urceObject.prop erty1 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
by: MSR |
last post by:
I have a couple of questions.
1. Copy Constructor.
class A {
private:
int a1;
double d1;
char *ptr;
|
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 be.
I assume what is meant is that a union member can't have
|
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, this bypasses the
problem or it seems to me like that.
Could any one give me some simple examples...
|
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
constructor, the destructor & operator=.
I wrote a small program to test if i can doa deep copy...
| |
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
|
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
|
by: Peter Olcott |
last post by:
Why can a union have a member with a copy constructor?
|
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 base. The
implementations do not have own data members. They only implement
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |