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

Difference between default copy constructor and user defined copy constructor

Hi,

We need copy constructor when we are passing the object to a function or if the function returns any object.
Suppose if we haven't defined the user defined copy constructor, it will give a call to default copy constructor. Won't it solve our purpose ?
Why we have to define the default copy constructor explicitely ??

Thanks,
Jeevan
Aug 24 '07 #1
4 13784
Meetee
931 Expert Mod 512MB
Hi,

We need copy constructor when we are passing the object to a function or if the function returns any object.
Suppose if we haven't defined the user defined copy constructor, it will give a call to default copy constructor. Won't it solve our purpose ?
Why we have to define the default copy constructor explicitely ??

Thanks,
Jeevan
Hi Jeevan,

C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field. So there is no need to define it explicitly. Default is default and not made defined by user.

Regards
Aug 24 '07 #2
JosAH
11,448 Expert 8TB
Hi Jeevan,

C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field. So there is no need to define it explicitly. Default is default and not made defined by user.

Regards
Whether or not there's a need for a user supplied copy ctor is entirely up on the
application. The default copy ctor builds a shallow copy of the original which may
not be what you want, i.e. maybe you need a deep copy instead in which case
you have to define a copy ctor yourself.

kind regards,

Jos
Aug 24 '07 #3
Banfa
9,065 Expert Mod 8TB
The default copy constructor does a 'shallow' copy, that is it copies the values of the members from 1 object to the other.

However if one of those members happens to be a pointer to allocated memory then both objects end up with pointers to the same piece of memory.

This is a problem, imagine that that memory is deleted in the destructor. When one of the objects is deleted the memory will be deleted but the other object still has a pointer to it so it now has an invalid pointer.

In this case a 'deep' copy is required, this is a copy that takes account of the types of the member variables and where required allocates and copies the allocated memory.

I deep copy has to be hand coded specifically for a class and that is when you would define your own opy constructor an discard the compilers auto generated deafult one.
Aug 24 '07 #4
hey Banfa, you should write a book. your ans was very clear. i now understand when i should define my own copy constructor. if you do write a book email me Primeinbox@outlook.com
May 20 '17 #5

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

Similar topics

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...
32
by: Mario Fratelli | last post by:
MyClass *p = new MyClass; MyClass *p = new MyClass(); do they mean something different? Thanks a lot, Mario Fratelli.
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
74
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...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
3
by: RainBow | last post by:
I understand that a compiler synthesises a default constructor if none is provided by the user ( of course depending on the situation if synthesis of such c'tor is actually needed in the program...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
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...
0
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...

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.