overloading assignment operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barbara Baby
    New Member
    • Oct 2011
    • 21

    overloading assignment operator

    I have code for the overloading assignment like this:

    Code:
    Stack& Stack::operator = (const Stack& rightOp)
       {
       if( this != &rightOp )
         {
         delete[] arraystack;
         arraysize = rightOp.arraysize;
         capacity = rightOp.capacity;
         arraystack = new int;
         strcpy( arraystack, rightOp.arraystack);
         }
       return *this;
       }
    The logic is:

    1. Check for self-assignment. If the address stored in the pointer this is the same as the address of the object rightOp, then skip to the final step.
    2. Delete the string array for the object that called the method.
    3. Set the string size for the object that called the method to the string size of rightOp.
    4. Set the string capacity for the object that called the method to the string capacity of rightOp.
    5. Use the string array pointer for the object that called the method to allocate an array of char. The size of the new string array should be the string capacity plus one.
    6. Copy the contents of the string array of rightOp into the string array of the object that called the method.
    7. Return *this.

    The problem is when compile this code there is warning regarding the strcpy.
    Anythig wrong with my code, please advise.Thank you
    Last edited by Meetee; Oct 24 '11, 04:40 AM. Reason: code tags added
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to allocate memory for this->arraystack before you strcpy into it.

    Use rightOp.arraysi ze to do that.

    Comment

    • Barbara Baby
      New Member
      • Oct 2011
      • 21

      #3
      Stack& Stack::operator = (const Stack& rightOp)
      {
      if( this != &rightOp )
      {
      delete[] arraystack;
      arraysize = rightOp.arraysi ze;
      capacity = rightOp.capacit y;
      arraystack = new int[capacity];
      strcpy( arraystack, rightOp.arrayst ack);
      }
      return *this;
      }

      Thank you for the advise, but how can I allocate memory for this->arraystack Please help.
      When I compile the above code, I got the following error:

      Stack.cpp: In copy constructor Stack::Stack(co nst Stack&):
      Stack.cpp:54: error: cannot convert int* to char* for argument 1 to char* strcpy(char*, const char*)

      Since my private data members are:

      int* arraystack;
      int capacity;
      int arraysize;
      int arraytop;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Your arraystack is an array of int. strcpy only copies C-style strings.

        You will need to copy each element of rightOP.arrayst ack to
        this->arraystack. Use a loop that runs fron 0 to rightOP.arraysi ze.

        Comment

        • Barbara Baby
          New Member
          • Oct 2011
          • 21

          #5
          Thank you very much. I got it now. You are the best!!!!

          Comment

          Working...