473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between default copy constructor and user defined copy constructor

1 New Member
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 13811
Meetee
931 Recognized Expert Moderator Contributor
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 Recognized Expert MVP
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 Recognized Expert Moderator Expert
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
Premier
1 New Member
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@outl ook.com
May 20 '17 #5

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

Similar topics

15
21179
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?
32
2710
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
4725
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5...
11
5702
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 difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public...
18
2240
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
74
15931
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
23
3642
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 the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and...
3
2259
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 e.g if vptr is needed, default c'tor must be synthesised by compiler). In the class Temporary below, there is no reason why a compiler should...
9
2884
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 Foo() is included, i believe. };
0
7824
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...
0
8176
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8191
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5370
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3816
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...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
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
1
1426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1154
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.