Skip to main content

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.

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.

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.