CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
Question :
We can overload assignment operator as a normal function.But we can not overload assignment operator as friend function why?




Category C++ Interview Questions
Rating (3.0) By 2 users
Added on 7/19/2006
Views 2196
Rate it!
Answers:

If the operation modifies the state of the class object, it operates on, it must be a member function, not a friend function Thus all operator such as =, *=, +=, etc are naturally defined as member functions not friend functions Conversely, if the operator does not modify any of its operands,but needs only a representation of the object, it does not have to be a member function and often less confusing.This is the reason why binary operators are often implemented as friend functions such as + , *, -, etc..



Becaus, Friend functions do not have a "this" pointer



 Posted by: akhilG    

Contact akhilG  Contact akhilG

obj1 = obj2;
Operator "=" can be overloaded either by the member function or by the friend operator function.

In case of the friend function, the obj1 and obj2 are should be from the different classes. If they are from the same class, the compilation error will occur. We can't use the "this" pointer in the friend function since it is not the member function of the class.

So,

If both objects are from the same class it is better to for operator function (operator=) as member function.

If the objects are from the different class, then the operator function as friend function is advisable.




 Posted by: mahendra kolluri    

Contact mahendra kolluri  Contact mahendra kolluri


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What are virtual functions?


View Answer
What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?

View Answer
What is a scope resolution operator?


View Answer
What do you mean by inheritance?


View Answer
What is the difference between an object and a class?


View Answer
What is encapsulation?


View Answer
what is defference between constructor and destructor


View Answer
Can you explain the term "resource acquisition is initialization?"

View Answer
What is the difference between operator new and the new operator?

View Answer
How can you force instantiation of a template?

View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C++ Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6143/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.75