【发布时间】:2017-03-28 19:58:00
【问题描述】:
我目前正在尝试掌握 C++ 中的模板和运算符重载。我写了一个类,它基本上是一个向量的包装器。我正在尝试将运算符重载添加到模板类中,以便可以遍历列表,但我不断收到重载运算符的分段错误。
这是模板代码。
template <class T> class List {
private:
std::list<T *> *objects;
int count;
uint currentIndex;
typename std::list<T*>::iterator item_iter;
public:
T *currentItem;
T *get(int idx);
void add(T *obj);
void start();
bool empty();
void next();
List<T>& operator++();
List() {
this->currentItem = NULL;
this->currentIndex = 0;
this->count = 0;
this->objects = new std::list<T *>;
}
~List() {
delete this->objects;
}
};
template <class T>
void List<T>::add(T *obj) {
this->objects->push_back(obj);
}
template <class T>
T *List<T>::get(int idx) {
if (idx < this->objects->size()) {
typename std::list<T*>::iterator it = this->objects->begin();
std::advance(it, idx);
return *it;
} else {
return NULL;
}
}
template <class T>
void List<T>::start() {
this->currentIndex = 0;
if (this->objects->size() > 0) {
this->item_iter = this->objects->begin();
this->currentItem = *(this->item_iter);
} else {
this->currentItem = NULL;
}
}
template <class T>
void List<T>::next() {
if (!this->empty() && (this->currentIndex == 0) && (this->currentItem == NULL)) {
this->start();
} else if (this->currentIndex < this->objects->size() - 1) {
this->currentIndex++;
std::advance(item_iter, 1);
this->currentItem = *item_iter;
} else {
this->currentItem = NULL;
}
}
template <class T>
bool List<T>::empty() {
return !this->objects->size();
}
template <class T>
List<T>& List<T>::operator++() {
// Note: We don't get this far
this->next();
return *this;
}
以下代码有效。
List<Item> *items = new List<Item>();
items->addItem(new Item(20));
for(items->start(); items->currentItem != NULL; items->next()) {
// Can work with items->currentItem;
}
以下代码导致分段错误。
List<Item> *items = new List<Item>();
items->addItem(new Item(20));
for(items->start(); items->currentItem != NULL; items++) {
// Segmentation fault occurs before we get inside
}
【问题讨论】:
-
在
for(items->start(); items->currentItem != NULL; items++)中,变量items是一个指针,所以items++递增指针。它不再指向有效实例。你的意思可能是(*iter)++? -
你为什么过度使用
new? C++ 不是 Java。你有大量的内存泄漏。 -
std::list<T *> *objects;不要那样做。就像,永远。这里根本不需要指针。T *currentItem;不要。void start();不要。void next();不要。这种界面非常很糟糕。即使是为了学习也不要这样做。 -
{List<int> list1; List<int> list2 = list1;}-- 如果要更多的孔,这个两行程序有双重删除错误。 -
@FrançoisAndrieux 你是对的,我需要 (*items)++ 并且运算符重载需要接受 int。
标签: c++ templates operator-overloading