473,385 Members | 1,279 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,385 software developers and data experts.

Copy constructur or bitwise copy

Hi I have a little c++ code but i'm not sure who makes the initialization for cp1.
Here is the code(in borlandc++ 3.1):
Expand|Select|Wrap|Line Numbers
  1. class A{
  2. int i;
  3. public:A(int val){i=val;
  4.                         }
  5.           A(A&a){i=a.i;
  6.                        }
  7. };
  8. void main()
  9. {
  10. A obj(1); A cp1(obj); A cp2=obj;
  11. }
  12.  
The question is how makes the initialization for cp1. and for cp2;
I belive that the initialization are made by de copy construct, but someone told me that the bitewise copy makes them. Please help me because I'm confused;
Sep 18 '07 #1
2 2863
Ganon11
3,652 Expert 2GB
When you have
Expand|Select|Wrap|Line Numbers
  1. A cp1(obj)
you are using the copy constructor.

When you have
Expand|Select|Wrap|Line Numbers
  1. A cp2 = obj;
you are using the assignment operator. Since you have not overloaded the assignment operator, the computer makes a bitwise copy of obj to cp2.
Sep 19 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
When you have

Code: ( cpp )
A cp2 = obj;

you are using the assignment operator.
Not true. You are still using the copy constructor.

The object cp2 is being created. Assignment only occurs after the object is created:

Expand|Select|Wrap|Line Numbers
  1. cp2 = obj;                 //assignment operator.
  2.  
Further, C++ does not use bitwise copy. It uses memberwise copy. The compiler's default copy constructor calls the copy constructor on each member of the class. You only need to write your own copy constructor when this won't work for your class. Typically, these will be youer classes that have pointer members.
Sep 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Gandalf | last post by:
Hello. If I have classes that allocates dynamic memory, it is a good thing to have a copy constructor(CC). If I have a CC, now I'm only trying to understand when I also need to overload the...
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: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ....then a bitwise object is created w/o calling...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
24
by: rdc02271 | last post by:
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...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
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...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.