5/11/09

Que What is an overloaded assignment operator ? What will happen if we assigned object to self( object1 =object1) ? What is the difference between cop

Whenever any assignment operation is performed on two already created objects of such class, assignment operator is invoked. By default compiler provides the assignment operator to each class.

Ex. MyClass obj1;

MyClass bj2;

obj2 = obj1;

Default assignment operator performs the member by member copy or shallow copy

1. Shallow Copy : If user doesn't provides the overloaded assignment operator, compiler provides one by default to the class. User needs to provide a overloaded assignment operator for a class, if the class is having any memory allocations to member pointers.

Ex, class MyClass {

int *m_pData ;

};

MyClass & operator=( Const MyClass & objectSource) {

m_pData = objectSource.m_pData;

return *this;

}

Problems Caused: If there are any memory allocations and user is not providing the user defined overloaded assignment operator, default overloaded assignment operator will be called. It will do a member by member shallow copy. it means it will assign the same memory allocated inside one object to the other object. Both object will point to the same memory location. If one object modify values for its own use, same values will be available to another object. This turned into using wrong values for another object.

If such objects will going to be destroyed. For first object the memory will be freed and second object will again try to free the already freed memory by object one. This may corrupt the hip.


To avoid above scenario, User defined overloaded assignment operator must be provided which provides the Deep Copy;

2. Deep Copy :

In deep copy first memory is allocated and then member values are copied.

MyClass & operator=( Const MyClass & objectSource) {

m_pData = new int;

*m_pData = *(objectSource.m_pData);

return *this;

}

Self assignment:

While writing a overloaded assignment operator, must ensure to check the self assignment.

Ex.

MyClass & operator=( Const MyClass & objectSource) {

m_pData = new int;

*m_pData = *(objectSource.m_pData);

return *this;

}

MyClass obj1;

obj1 = obj1;

If a self assignment is done, it will call overloaded assignment operator which will call again overloaded assignment operator. Thus the calling will be recursive and control will come back only after stack overflow.

Avoid this add a simple check.

MyClass & operator=( Const MyClass & objectSource) {

if( &objectSource == this)

return *this;

m_pData = new int;

*m_pData = *(objectSource.m_pData);

return *this;

}

difference between copy constructor and overloaded assignment operator - refer answer to previous question below.

2 comments:

Anonymous said...

>MyClass obj1;
>obj1 = obj1;

>"If a self assignment is done, it will call overloaded assignment operator which will call again overloaded assignment operator. Thus the calling will be recursive and control will come back only after stack overflow."

This is not true,Why assignment operator has to fall in recursive.

I have tried with the sample.

Sumedh said...

Ok, Implement your own overloded assignment operator for the class you have written, then set a break point inside this overloaded assignment operator. And start debugging.

It seems you tried with compiler provided assignment operator.

Let me know if you are still not able to see this behaviour.

ITUCU