Why a "operator=(...)" when there is a copy ctor?

Answers were Sorted based on User's Feedback



Why a "operator=(...)" when there is a copy ctor?..

Answer / guest

You use the assignment operator (operator = ()) whenever an
existing object is to be replaced with a different object.
The copy constructor X(const X&) is used to create a new
instance of an X-object exactly like another.

Notice the subtle difference. Assignment changes an existing
object while construction creates a new object. You can view
assignment as the application of a destructor, to flush away
the existing object, followed by a copy construction, to
make an exact copy of the assigned object.

Is This Answer Correct ?    4 Yes 0 No

Why a "operator=(...)" when there is a copy ctor?..

Answer / arun

Copy Constructor means creation of new object and after that
copy properties of some exiting object to newly created object.

Overloading assignment operator is that copy properties of
some exiting object to another exiting object of same type.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More OOPS Interview Questions

#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

0 Answers  


i^=j; j^=i; i^=j; value of i,j

1 Answers  


Can we have a private constructor ?

12 Answers   HSBC, Ness Technologies, TCS, Wipro,


why c++ is a highlevel language

3 Answers   Satyam, Tech Mahindra,


When is a memory allocated to a class?

11 Answers  






IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?

0 Answers  


A file pointer always contains the __________ of the file

5 Answers  


can we make a class static without using static keyword?

5 Answers   Aspire,


What is polymorphism ? Explain with examples

8 Answers   Ness Technologies,


Can we have inheritance without polymorphism?

0 Answers  


Templates mean

0 Answers  


write a program that takes input in digits and display the result in words from 1 to 1000

0 Answers   Wipro,


Categories