Overloading the =Operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UofFprogrammer
    New Member
    • Sep 2007
    • 14

    Overloading the =Operator

    Hello,
    I was taking a look at overloading the = operator in a C Plus Plus book. Part of the code of the overloaded =operator included checking for the case when the same object was on both sides of the equal sign. When and why would you have a situation like this?
    Code:
    someobject1=someobject1;
    Thank You
  • Cucumber
    New Member
    • Sep 2007
    • 90

    #2
    Originally posted by UofFprogrammer
    Hello,
    I was taking a look at overloading the = operator in a C Plus Plus book. Part of the code of the overloaded =operator included checking for the case when the same object was on both sides of the equal sign. When and why would you have a situation like this?
    Code:
    someobject1=someobject1;
    Thank You
    yes
    thats why that safety code is included in the operator=

    Comment

    • UofFprogrammer
      New Member
      • Sep 2007
      • 14

      #3
      I understand the safety code part, but I couldn't think of an intentional situation where the code would have same object on both sides of the assignment operator.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by UofFprogrammer
        When and why would you have a situation like this?

        Code: ( text )
        someobject1=som eobject1;
        You can't prevent some nutcase from doing this so you have to code defensively.

        This is important where objects with dynamically allocated assets are concerned. The left object has to delete its contents in preparation for a copy of the right object's contents. In this bizarre case the object would have deleted its own contents and then crashed the program trying to make a copy of them.

        It's easy enough to avoid the whole mess by just disallowing self-assignment.

        Comment

        • UofFprogrammer
          New Member
          • Sep 2007
          • 14

          #5
          Originally posted by weaknessforcats
          You can't prevent some nutcase from doing this so you have to code defensively.

          This is important where objects with dynamically allocated assets are concerned. The left object has to delete its contents in preparation for a copy of the right object's contents. In this bizarre case the object would have deleted its own contents and then crashed the program trying to make a copy of them.

          It's easy enough to avoid the whole mess by just disallowing self-assignment.
          Weaknessforcats , you hit the nail on the head. That was the situation that was in the book with dynamically allocated assets being deleted before being reassigned. I was curious if there was ever a situation where that code would be done intentionally or if it was just programmer error.

          Thanks for the response!

          Comment

          Working...