Does the default copy-constructor do a shallow or a deep copy in C++?
I am really confused with default copy constructor in cpp, as it does shallow copy or deep copy, as when I did v2=v1;
suppose v1={1,2,3}
, now if I have done v2[0]=1;
It does not get reflected but I heard it does shallow copy, can anybody please explain?
It doesn't do either. It does a memberwise copy. I.e. it copies all the members of the class using their copy constructors. If those members have copy constructors that do a deep copy then you'll get a deep copy, if they do a shallow copy then you'll get a shallow copy, or they could do something else entirely.
Deep copy and shallow copy are not C++ concepts, instead C++ lets you do a deep or shallow copy as you prefer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With