简体   繁体   English

在C ++中重载Stack的赋值运算符

[英]Overloading assignment operator for Stack in C++

I am trying to overload the assignment operator for my Stack class (in C++). 我试图重载我的Stack类的赋值运算符(在C ++中)。 When I run my output, I get the following: 运行输出时,将得到以下信息:

0 1 2 3 4 5 6 7 (empty) Freeing memory! 0 1 2 3 4 5 6 7(空)释放内存! Freeing memory! 释放内存!

The first line represents Stack a, the second line represents Stack b (see main()) and the "Freeing memory!" 第一行代表堆栈a,第二行代表堆栈b(请参阅main())和“释放内存!”。 is the destructor. 是破坏者。 For whatever reason Stack a's contents are not copying into Stack b's contents. 无论出于什么原因,堆栈a的内容都不会复制到堆栈b的内容中。 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::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. b=a正在调用赋值运算符,而不是副本构造函数。 Your supplied code example doesn't include an assignment operator, 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. 如果要使用复制构造函数,则需要使用注释的代码而不是赋值( Stack b(a) ),因为如果使用b = a ,则编译器将调用operator =,并且会收到内存泄漏错误不要在指针stk上应用深层复制,但是它想删除指针并在析构函数中释放内存,即,它不为指针分配内存。 If you want to use = you have to assign it in the same line as you are creating your object: Stack b = a . 如果要使用= ,则必须在创建对象的同一行中分配它: Stack b = a This is equivalent to Stack b(a) . 这等效于Stack b(a)
  2. Also, you need to use your source.top in the loop. 另外,您需要在循环中使用source.top You might want to do it by leaving top = source.top; 您可能需要保留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. 此外,请确保堆栈top索引中是否有任何值,或者它是否指向堆栈中的空白点。 Based on that you need to decide if you need to leave = sign in your for loop in your copy constructor or not. 基于此,您需要确定是否需要=在复制构造函数中登录for循环。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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