Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 15|Likes:
Published by Fenil Desai

More info:

Published by: Fenil Desai on May 19, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less





Copy constructoris a constructor function with the same name as the classused to make deep copy of objects.There are 3 important places where a copy constructor is called.When an object is created from another object of the same typeWhen an object is passed by value as a parameter to a functionWhen an object is returned from a functionIf a copy constructor is not defined in a class, the compiler itself defines one.This will ensure a shallow copy. If the class does not have pointer variables withdynamically allocated memory, then one need not worry about defining a copyconstructor. It can be left to the compiler's discretion.But if the class has pointer variables and has some dynamic memory allocations,then it is a must to have a copy constructor.--------------------------------------------------------------------------------For ex:class A //Without copy constructor{private:int x;public:A() {A = 10;}~A() {}}--------------------------------------------------------------------------------class B //With copy constructor{private:char *name;public:B(){name = new char[20];}~B(){delete name[];}//Copy constructorB(const B &b){name = new char[20];strcpy(name, b.name);}};--------------------------------------------------------------------------------Let us Imagine if you don't have a copy constructor for the class B. At the firstplace, if an object is created from some existing object, we cannot be sure thatthe memory is allocated. Also, if the memory is deleted in destructor, the deleteoperator might be called twice for the same memory location.This is a major risk. One happy thing is, if the class is not so complex this willcome to the fore during development itself. But if the class is very complicated,

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->