Thursday, July 9, 2009

What is the difference between copy constructor and assingment operator in C++ ?

Explain with an example.

What is the difference between copy constructor and assingment operator in C++ ?
no, it has nothing to do with pointers, and


no you don't have to declare a copy constructor - it is always available by default (often times, you may need to declare a private one to avoid the default being implictly invoked accidently). and


yes, you can use a copy constructor for primitive types too, not just classes.


The difference is that a copy constructor is a function, and an assignment is one of the ways to invoke it.





For example:





some_class b;


some_class a = b;





This is anassignment, thatr invokes a copy constructor of some_class;





some_class b;


some_class %26amp;a=b;





This assignment does not invoke a copy consturctor, because, a is now a reference to some_class instance, not an object.





void some_function(some_class a) {};





some_class b;


some_function(b);





This function call is not an assignment, but it will invoke acopy constructor of some_class, because the function parameter is declared to be passed by copy.


If the function was declared like


void some_function (some_class %26amp;a)





then the same function call would not invoke a copy constructor, because the value would be passed be reference.





Finally, a copy consturctor is just a regular constructor, like any other, only difference being that (1) as I said above, it is always available (and defaults to byte-too-byte copy), even when you don't define it, and (2) it takes a single argument, which is a reference to the same type.


For example:





some_class (some_class %26amp;a):foo(a.foo),bar(a.bar) {}


Could be a copy constructor for some_class, if this class had variables foo and bar,
Reply:the difference between a copy constructor and assignment operator is that


copy constructor is used for only objects where as assignment operator can be used for built in types and objects also.


In elaborate


copy constructor is used to copy the objects of the same class


i.e. for example


class rational


{


int real,imag


..................


...................//all variable and methods are declared including copy constructor also





}


void main()


{


rational r1,r2;


r1=r2;//here the contents of r2 will be sent to r2 means


// r1.real= r2.real %26amp; r1.imag= r2.imag


}


note:the above assignment will be vaild only if you have declared a copy constructor otherwise it will raise a error.


where as assignment operator can be used for built in types
Reply:I do not agree with any of the answers given above. A copy constructor copies the values of one object to the other object at the time of the declaration. A copy constructor will take an object as parameter. Its a special kind of parameterised constructor that takes an object instead of any variable or value. This will work with overloading the constructor. Consider the following example





class code


{


int id;


public:


code(int a) { id = a; }


code(code x)


{ id=x.id; }


void display()


{ cout %26lt;%26lt;id; }





};





The second constructor is copy constructor and the first is normal constructor with arguments.





void main()


{ code A(100);//invokes first constructor


code B(A); //invokes second constructor(copy constructor)


code C=A;//Assignment operator


---------


----------


---------


}





Hope this will help you in understanding the concept.
Reply:Please refer to the book by Lippman and Stroustrup or see online for information
Reply:Assignment operator and copy constructors are not the same.





Copy constructor is of major use when you handle pointers.





Consider you have a class c1 which has a pointer p1. Consider you have created an object o1 for this class c1 with the pointer p1 pointing to a memory location m1.





If you use assignment operator and create another object say, c1 o2 = 01; then the pointer p1 in o2 also will be pointing to the same memory location m1. If you make any changes to the memory location m1 through o1, it will be affected in object o2 also. This type of copying objects is known as shallow copy where the pointers are copied instead of the values to which they point ti.





If you use copy constructor, you can manually copy all values. So, if you want to create an object o2 similar to object o1, in the copy constructor, you create a new memory location m2 and make the pointer p1 of object o2 to point to m2. Both m1 and m2 will point to different locations but the locations to which they oint have same values. So, if you change value in one object, it will not be changed in the other object. This type of copy is known as deep copy.

columbine

No comments:

Post a Comment