Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is a "Copy Constructor"?

Answer Posted / phool chand

A Copy Constructor constructs a new object as a copy of an existing object of the same type. Frequently copy constructors do a 'deep copy' of the object. X( const X& X_object ){...}; is a copy constructor for class X.

Deep Copy vs. Shallow Copy:

a shallow copy simply copies the contents of an object directly - if the object contains pointers, both the old copy and the new copy contain pointers to the same actual item. In a deep copy, when an object contains a pointer, a new copy of whatever the pointer points AT is created and the new object contains a pointer to the newly created copy of the item.

Why are deep copies important? If you carry out a shallow copy you end up with two pointers to the same item. If that item is an object with a destructor, this generally means you'll end up calling the destructor for that item twice, which will generally cause problems.

Unfortunately, most don't know to ask this question directly: the symptom is generally heap corruption which is hard to track down directly since there it has many possible causes.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Distinguish between a # include and #define.

1132


How did c++ get its name?

1018


Is c++ an integer?

1017


How long will it take to learn programming?

1043


What is #include math h in c++?

1066


Why do we use setw in c++?

1101


How do I get good at c++ programming?

1145


What is ios :: in in c++?

1055


How do I start a c++ project?

1061


What is the extraction operator and what does it do?

1076


What do you mean by vtable and vptr in c++?

1035


If a base class declares a function to be virtual, and a derived class does not use the term virtual when overriding that class, is it still virtual when inherited by a third-generation class?

1014


What is static class data?

1036


What is runtime polymorphism in c++?

1145


What are the rules about using an underscore in a c++ identifier?

1159