Like Tree1Likes

Overloading assignment operator

This is a discussion on Overloading assignment operator within the C++ Programming forums, part of the General Programming Boards category; Code: IntArray::IntArray(const IntArray& h){ lo = h.lo; hi = h.hi; first = h.first; second = h.second; len = h.len; arr ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Code:
    IntArray::IntArray(const IntArray& h){
        lo = h.lo;
        hi = h.hi;
        first = h.first;
        second = h.second;
        len = h.len;
        arr = h.arr;
    }
    Seems like a mistake. arr is a pointer so if you do just a plain assignment you end up with two pointers sharing the memory. Since you are making a new object, this seems wrong. The pointer, h.arr, could be deleted, affecting this->arr.

    One way to fix it is to make a deep copy:
    Code:
    arr = new int[h.len];
    for (int i = 0; i < len; i++) {
       arr[i] = h.arr[i];
    }
    Just like that. Now each new object has its own copy of arr.

    Another way to fix it is to use shared_ptr, but you have to want the semantics of that type, and I'm not sure that you do.

  2. #17
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Quote Originally Posted by whiteflags View Post
    Ok well I maintain my advice, for the most part. Even discounting my misgivings about the design, you need to modify the current object (pointed to by this, if that wasn't clear before) to get anywhere. Your code does not modify the current object in any way. All of the changes you want are made to temp, not this. Temp is destroyed by the time operator = returns, so that doesn't help matters.
    I read up some more on the this pointer and understand what you mean now. I thought my code worked but it actually didn't. I changed it to this:
    Code:
    IntArray& IntArray::operator=(const IntArray& h){
        if (len != h.len)
            hault();
        else if(len == h.len){
            for(int i = 0; i < len; i++){
                arr[i] = h.arr[i];
            }
        }
        return *this;
    }
    Now it works great. I know the assignment is a little obscure and pointless, but I think he just wants us to understand how overloading operators work.
    Just a quick question on the this pointer...So when you are returning *this, it is the same thing as returning the IntArray object on the left hand side of the assignment operator?

    Elysia - Thanks for pointing that out, it fixed another problem I was having.

    EDIT: Just saw your post whiteflags...I didn't realize that, thanks for pointing it out also!
    Last edited by ryanmcclure4; 10-27-2012 at 08:17 PM.

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Just a quick question on the this pointer...So when you are returning *this, it is the same thing as returning the IntArray object on the left hand side of the assignment operator?
    No. The assignment operator has changed the current object (the object on the left hand side) to be like the object on the right hand side.
    Code:
    a = b;
    a.operator=(b);
    Same thing really. So what you return is in fact a after the changes are made.

  4. #19
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Ah alright, that makes sense.
    Thanks for all the help everyone! It's much appreciated

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by ryanmcclure4 View Post
    ...So when you are returning *this, it is the same thing as returning the IntArray object on the left hand side of the assignment operator?
    It is, but it is also returning the current instance you are working on.
    So

    a = b

    would invoke a.operator = (b)

    which would return a reference to the left-hand side, a.
    That allows you to chain it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,181
    O_o

    Elaborating on "chaining" (I'm elaborating because people have a disturbing tendency to get it wrong in their minds.):

    Code:
    a = b = c;
    Code:
    a.operator=(b.operator=(c));
    Code:
    a << b << c;
    Code:
    (a.operator<<(b)).operator<<(c);
    It happens this way because of operator precedence and associativity.

    It is easy to get tripped up on that so stick that phrase into your favorite search engine and bookmark it for when it does cause you a problem.

    Soma
    Last edited by phantomotap; 10-28-2012 at 09:47 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment operator overloading problem
    By auralius in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2011, 02:33 AM
  2. Overloading Assignment Operator, this pointer
    By cuo741 in forum C++ Programming
    Replies: 11
    Last Post: 12-09-2010, 08:12 AM
  3. Copy Constructors and Assignment Operator overloading.
    By leeor_net in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2009, 09:26 PM
  4. Assignment operator overloading
    By hsgw in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2007, 05:44 PM
  5. overloading the assignment operator
    By Unregistered36 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2001, 05:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21