What is the difference between a copy constructor and an
overloaded assignment operator?

Answer Posted / neelkamal yadav

A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

First, the applicant must know that a copy constructor is one that has only one argument of the same type as the constructor. The compiler invokes a copy constructor wherever it needs to make a copy of the object, for example to pass an argument by value. If you do not provide a copy constructor, the compiler creates a member- by-member copy constructor for you.

You can write overloaded assignment operators that take arguments of other classes, but that behavior is usually implemented with implicit conversion constructors. If you do not provide an overloaded assignment operator for the class, the compiler creates a default member- by-member assignment operator.

This discussion is a good place to get into why classes need copy constructors and overloaded assignment operators. If the applicant discusses these with respect to data member pointers that point to dynamically allocated resources, the applicant probably has a good grasp of the problem.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How would you use the functions randomize() and random()?

603


What are the benefits of operator overloading?

654


What is the difference between struct and class?

714


What is #include ctype h in c++?

636


What is pointer to member?

583






Explain Memory Allocation in C/C++ ?

608


Is it possible to have a recursive inline function in c++?

521


Do you know the problem with overriding functions?

547


What is input operator in c++?

561


What is a rooted hierarchy?

661


What does it mean to declare a destructor as static?

596


How does class accomplish data hiding in c++?

629


What is a down cast?

579


Write about the use of the virtual destructor?

585


Of the numbers 12 23 9 28 which would be at the top of a properly implemented maxheap a) 28 b) 9 c) Any of them could be

891