Hi all,
Differentiate between copy constructor and assignment operator?
Explain each with an example.
Thanks in advance....
9 2211
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
A copy constructor uses the data already in another instance of this class to initialize the variables in the new object - MyClass obj1("hello");
-
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 - MyClass obj1("hello");
-
MyClass obj2("banana");
-
-
obj2 = obj1;
the '=' operator must be overloaded to accomodate this assignment.
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.
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 :)
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.
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.
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.
Hi all,
Differentiate between copy constructor and assignment operator?
Explain each with an example.
Thanks in advance....
Has someone just done someone's homework?
Has someone just done someone's homework?
Dammit you're right!!
I usually avoid those and didn't see it.
Glad somebody is awake :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
>>>
|
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...
|
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, ...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |