![](/img/trans.png)
[英]Linked List: How to implement Destructor, Copy Constructor, and Copy Assignment Operator?
[英]implementing a copy constructor, destructor, and how to overload assignment operator for queues
這就是我所擁有的,但我不確定這是否有效。 我不確定以這種方式復制是否有效,而且我想不出一種超載的方法。 復制時,我給兩個相同的大小,將 header 和 tail(第一個和最后一個元素的索引)設置到同一個位置,然后簡單地將數組的每個元素復制到另一個。 重載在這里似乎很棘手。 我以前做過重載,但從來沒有使用循環數組隊列。
#include <iostream>
using namespace std;
class CircArrayQueue
{
private:
int *arr;
int head;
int tail;
int size;
CircArrayQueue()
{
arr = new int[5];
head = -1;
tail = -1;
}
CircArrayQueue(int s)
{
arr = new int[s];
head = -1;
tail = -1;
size = s;
}
void copyQueue(CircArrayQueue &object)
{
object.size = this->size;
for (int i = 0; i < this->size; i++)
{
object.arr[i] = this->arr[i];
}
object.head = this->head;
object.tail = this->tail;
}
~CircArrayQueue()
{
delete[] arr;
}
};
我將學習的完美解決方案是復制交換習語,它可以完成工作。 什么是復制和交換成語?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.