“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Copy contructor is a special type of constructor used in C++ language to create a copy of object. If we dont explicitly declare, the compiler creates one by default. This default constructor doesnt do much except for byte-by-byte copy. In special cases, where class owns a pointer or file-descriptor it is not generally recommended to have byte-by-byte copy. In such cases, developer has to create a constructor customized for the use.
For example, in case Class A has a member which is pointer to an int. If default copy-constructor is used,
A obj2 = obj1 ;
obj2’s member pointer will point to the same memory location pointed by obj1’s pointer. This can later cause problems like memory leak, or dangling pointer.
So in such cases, if developer writes his own copy-constructor and makes sure that obj2’s pointer now pointes to a diff memory location which has the same data value as in obj1’s pointer.
A::A (const A&) {
int *ptrMember = new int ;
*ptrMember= *A.ptrMember
}
The difference between the copy constructor and assignment operator is that the copy constructor is a constructor — a function whose job it is to turn raw storage into an object of a specific class. An assignment operator, on the other hand, copies state between two existing objects. In other words, an assignment operator has to take into account the current state of the object when copying the other object’s state into it. The copy constructor is creating a new object from raw storage and knows it’s writing over garbage.
Solution by Ayush does not give the answer to the question rather it explains the copy constructor.
Basic difference between copy constructor and assignment operator is the time when it is used..
Copy constructor as name suggests is used when the object is created. And it copies the contents of another object of same type byte by byte to its member variables.
On the other hand assignment operator which does the same operation as the copy constructor but can be called anytime in the life of the program..
I disagree with madhura on the statement that “assignment operator has to take into account teh current state of the object”.. No assigment operation ever takes into account the surrent state of the object.. If there is valid memory created for the object (which would be true, since you are calling assignment operation) it just overwrites the contents of the member variables..
An assignment operator can also write in a storage which is garbage.. Consider a class which only has default constructor provided by the compiler.. When an object is created mem is allocated but members are not initialised.. In that case when you perform an assignment operation on the object, it is writing over garbage..
I dont understand why 9 votes to first answer, am not saying its wrong, but its not the explaination for question. And regarding the problem he reported (byte by byte), we will have that problem everywhere we try to copy byte by byte the variable or objects which is treated or created as ponter
Assignment operator will not create the object for u,whereas copyconstructor will create a object for u.
And moreover copyconstructor will copy the entire object. coming to the assignment operator will just refering the object it is not exactly contain the true contact just it will give reference.
When Madhura (4) says “an assignment operator has to take into account the current state of the object when copying the other object’s state into it”, that is correct because existing members may need to be deleted or deconstructed.
If I might add a little more ….
assertion::
An assignment operator can always be wirtten
as a destructor followed by a copy constructor.
E.g.
class A
{
A(const A& a) { /* deep copy */ }
operator=(const A& a)
{
this->~A();
new(this) A(a);
}
}
copy constructor creates new object while the assignmetn opertor used for existing objects.
Copy contructor is a special type of constructor used in C++ language to create a copy of object. If we dont explicitly declare, the compiler creates one by default. This default constructor doesnt do much except for byte-by-byte copy. In special cases, where class owns a pointer or file-descriptor it is not generally recommended to have byte-by-byte copy. In such cases, developer has to create a constructor customized for the use.
For example, in case Class A has a member which is pointer to an int. If default copy-constructor is used,
A obj2 = obj1 ;
obj2’s member pointer will point to the same memory location pointed by obj1’s pointer. This can later cause problems like memory leak, or dangling pointer.
So in such cases, if developer writes his own copy-constructor and makes sure that obj2’s pointer now pointes to a diff memory location which has the same data value as in obj1’s pointer.
A::A (const A&) {
int *ptrMember = new int ;
*ptrMember= *A.ptrMember
}
The difference between the copy constructor and assignment operator is that the copy constructor is a constructor — a function whose job it is to turn raw storage into an object of a specific class. An assignment operator, on the other hand, copies state between two existing objects. In other words, an assignment operator has to take into account the current state of the object when copying the other object’s state into it. The copy constructor is creating a new object from raw storage and knows it’s writing over garbage.
Solution by Ayush does not give the answer to the question rather it explains the copy constructor.
Basic difference between copy constructor and assignment operator is the time when it is used..
Copy constructor as name suggests is used when the object is created. And it copies the contents of another object of same type byte by byte to its member variables.
On the other hand assignment operator which does the same operation as the copy constructor but can be called anytime in the life of the program..
I disagree with madhura on the statement that “assignment operator has to take into account teh current state of the object”.. No assigment operation ever takes into account the surrent state of the object.. If there is valid memory created for the object (which would be true, since you are calling assignment operation) it just overwrites the contents of the member variables..
An assignment operator can also write in a storage which is garbage.. Consider a class which only has default constructor provided by the compiler.. When an object is created mem is allocated but members are not initialised.. In that case when you perform an assignment operation on the object, it is writing over garbage..
Also copy contructors are called when you call a function passing objects by value or returning by value.
I dont understand why 9 votes to first answer, am not saying its wrong, but its not the explaination for question. And regarding the problem he reported (byte by byte), we will have that problem everywhere we try to copy byte by byte the variable or objects which is treated or created as ponter
Assignment operator will not create the object for u,whereas copyconstructor will create a object for u.
And moreover copyconstructor will copy the entire object. coming to the assignment operator will just refering the object it is not exactly contain the true contact just it will give reference.
When Madhura (4) says “an assignment operator has to take into account the current state of the object when copying the other object’s state into it”, that is correct because existing members may need to be deleted or deconstructed.
If I might add a little more ….
assertion::
An assignment operator can always be wirtten
as a destructor followed by a copy constructor.
E.g.
class A
{
A(const A& a) { /* deep copy */ }
operator=(const A& a)
{
this->~A();
new(this) A(a);
}
}
Sorry, the assignment operator declaration syntax should have been
A& operator=(const A& a)
Copy constuctor is special type of constructor, that copy the address of the previous obj;
For Ex: using in console application:
class staff
{
int Stid; string post;
public staff() // Default constructor
{
Console.WriteLine(”Enter the staff id:”);
Stid = int.Parse(Console.ReadLine());
Console.WriteLine(”Enter the staff name:”);
post = Console.ReadLine();
}
public staff(staff copy) // copy constructor
{
Stid = copy.Stid;
position = copy.position;
}
public void display()
{
Console.WriteLine(” staff id:” + Stid);
Console.WriteLine(”staff position:” + position);
Console.ReadLine();
}
Leave an Answer/Comment