简体   繁体   中英

Overloading assignment operator for Stack in C++

I am trying to overload the assignment operator for my Stack class (in C++). When I run my output, I get the following:

0 1 2 3 4 5 6 7 (empty) Freeing memory! Freeing memory!

The first line represents Stack a, the second line represents Stack b (see main()) and the "Freeing memory!" is the destructor. For whatever reason Stack a's contents are not copying into Stack b's contents. I have verified that the capacity space is there (did a bunch of print statements) but the copying is not happening. Can someone help?

Here is my Stack constructor, copy constructor, and destructor:

/********************************/
Stack::Stack()
{
    top = -1; // array counter starts at 0
    stk = new int[MAXSIZE];
    capacity = MAXSIZE;
}
/********************************/

/********copy constructor ***********/
Stack::Stack(const Stack& source) {
  stk = new int[source.capacity]; // allocates new array space for the copy constructor
   for (int i = 0; i <= top; i++) {
      stk[i] = source.stk[i];
    }
    top = source.top;
    capacity = source.capacity;
} /******* end copy constructor *******/

Stack::~Stack() {
    cout << "Freeing memory!" << endl;
    delete[] stk;
}


Here is my main:


int main() {

    Stack a;
    for (int i=0; i < 8; i++) {
        a.push(i);
    }
    //cout << "\n Using copying incorrectly...\n";
    //Stack b(a);
    Stack b;
    b = a;
    a.display();
    b.display();

b=a is invoking the assignment operator, not the copy constructor. Your supplied code example doesn't include an assignment operator, operator=() . Your assignment operator should include essentially the same logic as your copy constructor so you'd be best to move that logic to a new function and have both the copy constructor and assignment operator call this new function.

You have two mistakes:

  1. If you want to fall into copy constructor you need to use your commented code not assignment ( Stack b(a) ), because, if you use b = a , compiler will call the operator= and you will get memory leak error as it does not apply deep copy on your pointer stk but it wants to delete the pointer and deallocate the memory in the destructor, ie it does not allocate memory for your pointer. If you want to use = you have to assign it in the same line as you are creating your object: Stack b = a . This is equivalent to Stack b(a) .
  2. Also, you need to use your source.top in the loop. You might want to do it by leaving top = source.top; before your loop.

Also, make sure, if you have any value on the top index of your stack or if it is pointing to an empty spot in your stack. Based on that you need to decide if you need to leave = sign in your for loop in your copy constructor or not.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM