473,499 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy costructor

13 New Member
Hi all,

Differentiate between copy constructor and assignment operator?

Explain each with an example.

Thanks in advance....
Jan 11 '07 #1
9 2211
willakawill
1,646 Top Contributor
Hi all,

Differentiate between copy constructor and assignment operator?

Explain each with an example.

Thanks in advance....
A constructor is used when a class is declared and parameters in brackets are used to initialize variables in the constructor

Expand|Select|Wrap|Line Numbers
  1. MyClass obj1("hello");
A copy constructor uses the data already in another instance of this class to initialize the variables in the new object

Expand|Select|Wrap|Line Numbers
  1. MyClass obj1("hello");
  2. Myclass ojb2(obj1);
An overloaded operator is used to copy the values in the member variables of one instance into the member variables of a second instance after both instances have been initialized

Expand|Select|Wrap|Line Numbers
  1. MyClass obj1("hello");
  2. MyClass obj2("banana");
  3.  
  4. obj2 = obj1;
the '=' operator must be overloaded to accomodate this assignment.
Jan 11 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
the '=' operator must be overloaded to accomodate this assignment.
Strictly speaking not quite true, the compiler will produce a default assignment operator (and copy constructor) if you don't explicitly write one. However it may not do the right thing as it just does a straight member to member copy and if some of the members are pointers with allocated memory you will end up with 2 classes both pointing to the same allocated memory. This will probably result in an exception at some point after 1 of the objects has been release (and as presumable freed the memory) when the other object tries to access it.
Jan 11 '07 #3
willakawill
1,646 Top Contributor
Strictly speaking not quite true, the compiler will produce a default assignment operator (and copy constructor) if you don't explicitly write one. However it may not do the right thing as it just does a straight member to member copy and if some of the members are pointers with allocated memory you will end up with 2 classes both pointing to the same allocated memory. This will probably result in an exception at some point after 1 of the objects has been release (and as presumable freed the memory) when the other object tries to access it.
Correct. So, strictly speaking, never use default constructors, copy constructors, destructors or operators. Unless, of course, you are fond of chasing random memory errors :)
Jan 11 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
Correct. So, strictly speaking, never use default constructors, copy constructors, destructors or operators. Unless, of course, you are fond of chasing random memory errors :)
Probably very wise advice, however people generally do not write copy constructors and assignment operators for all there structures which they would have to in order to follow it.
Jan 11 '07 #5
Ganon11
3,652 Recognized Expert Specialist
In general, a copy constructor, assignment operator, and destructor must be written for classes involving pointers to avoid shallow copy of pointers (as Banfa described above). I'm not sure they're 100% necessary for pointer-less classes, but if your problem specification requires special assignment, copy, etc, then it's not a bad thing to overload these operations/constructors.
Jan 11 '07 #6
willakawill
1,646 Top Contributor
Probably very wise advice, however people generally do not write copy constructors and assignment operators for all there structures which they would have to in order to follow it.
And who are these 'people' for whom you speak? :)

Of course I would only write a constructor, copy constructor, destructor, overloaded operator in circumstances that demand their use. Never just for the sake of it.
Jan 11 '07 #7
r035198x
13,262 MVP
Hi all,

Differentiate between copy constructor and assignment operator?

Explain each with an example.

Thanks in advance....
Has someone just done someone's homework?
Jan 12 '07 #8
willakawill
1,646 Top Contributor
Has someone just done someone's homework?
Dammit you're right!!
I usually avoid those and didn't see it.
Glad somebody is awake :)
Jan 12 '07 #9
Geevi
13 New Member
Thanks to all,
Jan 12 '07 #10

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

Similar topics

42
5719
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
21170
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...
2
12339
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
24
3596
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...
10
2522
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, ...
22
3587
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
7134
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,...
0
7392
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
5479
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 project—planning, coding, testing,...
0
4605
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...
0
3105
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...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
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...

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.