By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Copy Constructor in C++

P: 2
Hi,
What is meant by deep copy and shallow copy in case of copy constructor in C++?
Aug 11 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hi,
What is meant by deep copy and shallow copy in case of copy constructor in C++?
The same as when just referring to an object, deep will make a copy of the object and its values, while shallow will return a reference to the memory location where the object being copied is at, and if any of the values are changed through one, they are thereby changed in the other as well.
Aug 11 '07 #2

Expert 10K+
P: 11,448
Here's an analogy: suppose I point my finger at my laptop; I own that laptop. Also
suppose you make a copy of me; my copy will point to that same laptop. That's
a shallow copy. Suppose when you make a copy of me you also make a copy of
everything I point to (or 'own'). My copy will point to its own copy of a laptop.
That's a deep copy. When my copy drops his laptop I still have mine. In the first
case (a shallow copy) neither my copy nor me have a laptop anymore when either
me or my copy drop the (single) laptop.

kind regards,

Jos
Aug 12 '07 #3

Post your reply

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