copy constructor

0 Saswat Prusty · June 12, 2014
Hi, anyone please help me with copy constructor and what is deep and shallow constructor?

Post a Reply


Oldest  Newest  Rating
0 Alexander Taylor · June 13, 2014

To sum up what the site says,

Shallow copying is basically the default c++ way of copying a class, that is to say it copies each member separately. - Read more for problems with this method

Deep copying is duplicates the object or variable being pointed to, which solves one of the problems with Shallow copying. - Again read the site for more information

-- The pages summary --

  • The default copy constructor and default assignment operators do shallow copies, which is fine for classes that contain no dynamically allocated variables.

  • Classes with dynamically allocated variables need to have a copy constructor and assignment operator that do a deep copy.

  • The assignment operator is usually implemented using the same code as the copy constructor, but it checks for self-assignment, returns *this, and deallocates any previously allocated memory before deep copying.

  • If you donâ??t want a class to be copyable, use a private copy constructor and assignment operator prototype in the class header.

  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator