473,725 Members | 2,169 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
24 3628
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::operato r[]? :>

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::operato r[]? :>


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=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 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::operato r[]? :>


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::operato r[] 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
21196
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
1984
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
4
7130
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
10
2574
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...
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 constructor, the destructor & operator=. I wrote a small program to test if i can doa deep copy...
74
16010
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
3966
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
6420
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
34
3673
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
0
8888
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 usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8752
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
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...
1
9176
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,...
0
8097
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, 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...
1
6702
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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 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.