为什么我Queue<listEntry<int> > queue2(intList);
被两次调用复制构造函数?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class dataType>
class listEntry
{
private:
dataType data;
public:
listEntry *next;
listEntry *prev;
dataType getData() { return this->data; }
listEntry();
listEntry(dataType data) { this->data = data; }
~listEntry() {};
};
template <class dataType>
class List
{
private:
dataType *head;
dataType *tail;
int count;
public:
dataType *getHead() { return this->head; }
dataType *getTail() { return this->tail; }
void addToTail(dataType *newEntry);
void addToHead(dataType *newEntry);
int getCount() { return count; }
void printListForward();
List();
List(const List<dataType> &li);
~List();
};
template <class dataType>
class Queue: public List<dataType>
{
public:
void enQueue(dataType *newEntry);
Queue():List<dataType>() { return; }
Queue(List<dataType> li):List<dataType>(li) { return; }
};
void addIntegersToList(Queue<listEntry<int> > *queue);
int main()
{
cout << "Queue of Integers: " << endl;
cout << "Default Queue Constructor: " << endl;
//Create an intance of a queue using the default queue constructor
Queue<listEntry<int> > queue;
addIntegersToList(&queue);
queue.printListForward();
cout << endl;
cout << "List parameter Queue Constructor: " << endl;
//Create a new list that will be used to initiate an instance of a queue
List<listEntry<int> > intList;
//Add some data to that list
intList.addToTail(new listEntry<int>(11));
//Now create a new instance of a queue with a list as its creation parameter
Queue<listEntry<int> > queue2(intList);
//addIntegersToList(&queue2);
//queue2.printListForward();
while (true);
}
template <class dataType>
List<dataType>::List()
{
this->head = NULL;
this->tail = NULL;
this->count = 0;
}
template <class dataType>
void List<dataType>::addToTail(dataType *newEntry)
{
newEntry->next = NULL;
newEntry->prev = NULL;
if (this->head == NULL)
{
this->head = newEntry;
this->tail = this->head;
this->count = this->count + 1;
}
else
{
this->tail->next = newEntry;
newEntry->prev = this->tail;
this->tail = newEntry;
this->count = this->count + 1;
}
dataType *temp = this->tail;
}
template <class dataType>
void List<dataType>::addToHead(dataType *newEntry)
{
newEntry->next = NULL;
newEntry->prev = NULL;
if (this->head == NULL)
{
this->head = newEntry;
this->tail = this->head;
}
else
{
this->head->prev = newEntry;
newEntry->next = this->head;
this->head = newEntry;
}
this->count = this->count + 1;
}
template <class dataType>
void List<dataType>::printListForward()
{
cout << "Head: ";
dataType *temp = this->head;
while (temp)
{
cout << temp->getData() << " -> ";
temp = temp->next;
}
cout << " :Tail" << endl;
}
void addIntegersToList(Queue<listEntry<int> > *queue)
{
int i = 0;
for (i = 1; i <= 10; i++)
queue->enQueue(new listEntry<int>(i));
}
template <class dataType>
void Queue<dataType>::enQueue(dataType *newEntry)
{
this->addToTail(newEntry);
}
template <class dataType>
List<dataType>::~List()
{
while (this->head)
{
dataType *next = this->head->next;
delete this->head;
this->count = this->count - 1;
this->head = next;
}
}
template <class dataType>
List<dataType>::List(const List<dataType> &li)
{
tail = new dataType;
*tail = *li.tail;
head = new dataType;
*head = *li.head;
cout << "head: " << head << endl;
cout << "tail: " << tail << endl;
count = li.count;
cout << "count: " << count << endl;
}
template <class dataType>
listEntry<dataType>::listEntry()
{
this->next = NULL;
this->prev = NULL;
this->data = 0;
}
答案 0 :(得分:1)
因为您正在按值将List
传递给Queue
构造函数。试试这个
Queue(const List<dataType>& li):List<dataType>(li) { return; }