Overloading assignment operator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Overloading assignment operator

 
0
  #1
Mar 18th, 2006
Why cant we overload assignment operator using a friend function?
Please help??
Thanks,
comwizz.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Overloading assignment operator

 
0
  #2
Mar 18th, 2006
Originally Posted by comwizz
Why cant we overload assignment operator using a friend function?
operator= can only be a member function. It is intimately connected to the object on the left side of the =.

If it was possible to define operator= globally, then you might attempt to redefine the built-in = sign
  1. int operator=(int, userdefinedtype);
This is the reason due to which you are forced to use operator= as member function.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Overloading assignment operator

 
0
  #3
Mar 18th, 2006
Originally Posted by sunnypalsingh
It is intimately connected to the object on the left side of the =
I dont understand this clearly.

If it was possible to define operator= globally, then you might attempt to redefine the built-in = sign
  1. int operator=(int, userdefinedtype);
I cannot understand this fully , how would globally defining = operator make a difference??
Thanks for your help,
comwizz
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Overloading assignment operator

 
0
  #4
Mar 18th, 2006
>operator= can only be a member function.
Correct.

>It is intimately connected to the object on the left side of the =.
Yes, but not so intimately (ie. constructor or destructor) that a non-member function is completely illogical. You could, if C++ allowed it, do this, and nobody would question how reasonable it is:
  1. class C {
  2. friend void operator= ( C& lhs, const C& rhs );
  3. };
  4.  
  5. void operator= ( C& lhs, const C& rhs )
  6. {
  7. // Assign rhs to lhs
  8. }
>If it was possible to define operator= globally, then you might attempt to redefine the built-in = sign
That's a different issue entirely. It's trivial for an implementation to allow a non-member operator= but disallow defining it on built-in types. The reason operator= is required to be a member is because operator= has default behavior. If you redefine it globally, there's a distinct threat of a = b meaning different things to different parts of the code. Consider this:
  1. struct C {
  2. // Default operator= (shallow copy)
  3. char *p;
  4. };
  5.  
  6. void foo ( C& c )
  7. {
  8. C scratch;
  9.  
  10. // Work with scratch
  11.  
  12. c = scratch;
  13. }
  14.  
  15. void operator= ( C& lhs, const C& rhs )
  16. {
  17. // Assign rhs to lhs (deep copy)
  18. }
  19.  
  20. void bar ( C& c )
  21. {
  22. C scratch;
  23.  
  24. // Work with scratch
  25.  
  26. c = scratch;
  27. }
  28.  
  29. int main()
  30. {
  31. C test;
  32.  
  33. foo ( test );
  34. bar ( test );
  35. }
foo doesn't know that operator= was redefined because function declarations are positional. If you declare an overload after you use the function, the overload isn't recognized. So foo will use the default operator=, which performs a shallow copy and ends up aliasing the pointer in c with the transient pointer in scratch that will get destroyed when foo returns. Therefore you have a memory leak, data loss, *and* an unexpected pointer to freed memory, whereas bar has none of these problems because it uses the overloaded operator= and correctly performs a deep copy.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Overloading assignment operator

 
0
  #5
Mar 18th, 2006
foo doesn't know that operator= was redefined because function declarations are positional. If you declare an overload after you use the function, the overload isn't recognized. So foo will use the default operator=, which performs a shallow copy and ends up aliasing the pointer in c with the transient pointer in scratch that will get destroyed when foo returns. Therefore you have a memory leak, data loss, *and* an unexpected pointer to freed memory, whereas bar has none of these problems because it uses the overloaded operator= and correctly performs a deep copy.
According to your code the assignment operator could be used as a friend function to class c and the bar function would work correctly as deep copy is implemented . So why not use the assignment operator overloading as a friend function by declaring and defining it above other functions and hence default = operator performing shallow copy would not be called??
Thanks for your help,
comwizz
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Overloading assignment operator

 
0
  #6
Mar 18th, 2006
>So why not use the assignment operator overloading as a friend function
>by declaring and defining it above other functions
Indeed, why not? Just make sure that you get it right for any possible combination of functions and declarations in any number and combination of source files. And also keep a sticky note on your monitor to remind you of such a subtle problem that not even lint can warn you about. Also, don't forget that you need to watch for it in other people's code and any code you maintain, for a seemingly innocent change could introduce serious and difficult to find bugs.

The point is that it's very difficult to error check this problem in any non-trivial project. It's also so subtle that even programmers who are aware of it will be hard pressed to avoid it, much less the more numerous programmers who wouldn't even know the problem exists until it bites them in the ass. Compare that to the current state of affairs where a simple rule avoids the problem entirely, and the simple rule can't be missed because it gives you a compile time error if you don't follow it.

A guiding rule in the design of C++ is that a compile time error is much more desirable than a runtime error.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Overloading assignment operator

 
0
  #7
Mar 19th, 2006
Thanks Narue.

Your posts are always eye openers.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC