制作一个类以提供Lists接口

Making a class to provide Lists interface

本文关键字:Lists 接口 一个      更新时间:2023-10-16

我正试图创建一个在库中使用的类,我甚至不确定它是否可能做到。这个类的想法是提供插入、搜索和删除列表中项目的方法,但我的主要问题是,由于我不知道我想放在列表中的对象的类型,我不知道如何使用它。我想我可以放一个void指针,然后让它指向一个结构,但我没能让它发挥作用。

假设我的课是这样的:

class ListManager
{
private:
void* FirstItem;//This would point to the first item of the list
void* LastItem;//This would point to the last item of the list
public:
void AddItemToList(void* Item);
void RemoveItemFromList(void* Item);
}

因此,我的想法是,从我的程序中,我可以定义一个类似的结构

struct Employee
{
*char Name;
int Id;
int PhoneNumber;
}

然后,使用此类,可以添加/删除员工。因此,在这种情况下,void*指针应该指向Employee类型的结构。尽管如此,我还是想让我的类适用于任何类型的结构。我不知道我是否准确地解释了我想做什么,我尝试了几种方法,但都失败了。

如果我没有正确解释自己的话,我将发布一个我希望课堂如何工作的代码

ListManager *Worker;
Worker=new(ListManager);
Employee *Item;
Item=new (Employee);
Item->Id=126;
Item->PhoneNumber=42154872;
Worker->AddItemToList(Item);
/*At this point, FirstItem and LastItem should point to the Item i just created*/

有人能给我指一个正确的方向吗,比如如何在不知道结构类型的情况下让一个班级使用结构?

提前感谢

您需要模板!这里有一个简单的界面,您可以开始使用它。

template <typename T>
class ListManager
{
public:
void addItemToList(const T& item);
void removeItemFromList(const T& item);
}

现在T是您的类型,您可以声明一个ListManager,如下所示:

ListManager<Employee> manager;

我还建议您查看stl文档/列表的实现:http://www.cplusplus.com/reference/list/list/您还需要深入研究迭代器的概念。此外,请尝试使用值而不是指针。使用我给你的接口,你将把实际值存储在列表中,而不是指针,所以列表拥有对象,你不需要手动管理内存。

请参阅本教程:http://www.tutorialspoint.com/cplusplus/cpp_templates.htm

你的例子可能看起来像:

#include <iostream>
using namespace std;
template <class T>
class ListManager
{
private:
void* FirstItem;//This would point to the first item of the list
void* LastItem;//This would point to the last item of the list
public:
void AddItemToList(const T& Item){
std::cout << Item << std::endl;
};
void RemoveItemFromList(const T& Item){};
};
int main() {
ListManager<std::string> mgr;
mgr.AddItemToList("Test");
return 0;
}

输出:

Test

工作示例:http://ideone.com/FCAtcJ

C++中确实没有接口,但您可以使用相同的类模板:

template <typename T>
class ListInterface {
public:
void add(T * item) { list.push_back( item); }
void remove(T * item) { list.erase(std::find(list.begin(), list.end(), item)); }
T * get(int index) { return list[index]; }
int size() const { return list.size(); }
private:
std::vector<T *> list;
};
class Manager : public ListInterface<Employee> {
// ...
};

当然,这是一个简单的例子,您确实没有在remove()get()方法中进行一些检查。

这个问题有多种解决方案。

只需使用std::list

std::list<Employee> manager;

如果1。不可能使用模板定义自己的列表界面

template <class T>
class ListManager
{
private:
class Iterator {
public:
T& item;
Iterator* next;
Iterator* prev;
Iterator(T& i, Iterator* n, Iterator* p) : item(i), next(n), prev(p) {}
}
Iterator* FirstItem = NULL;//This would point to the first item of the list
Iterator* LastItem = NULL;//This would point to the last item of the list
public:
void AddItemToList(T& Item) {
if(LastItem) {
Iterator* it = new Iterator{Item, NULL, LastItem};
LastItem->next = it;
LastItem = it;
} else {
Iterator* it = new Iterator{Item, NULL, NULL};
FirstItem = it;
LastItem = it;
}
}
void RemoveItemFromList(const T& Item) { 
for(Iterator* it = FirstItem; it != NULL; it = it->next) {
if(it->item == Item) {
if(it->prev)
it->prev->next = it->next;
else
FirstItem = it->next;
if(it->next)
it->next->pref = it->pref;
else
LastItem = it->pref;
delete it;
break;
}
}
}
};

如果1。和2。不可能,因为您的列表中需要多个类型。您可以使用Any列表(如boost::Any)或带有void*:的不安全版本

class ListManager
{
private:
class Iterator {
public:
void* item;
Iterator* next;
Iterator* prev;
Iterator(void* i, Iterator* n, Iterator* p) : item(i), next(n), prev(p) {}
}
Iterator* FirstItem = NULL;//This would point to the first item of the list
Iterator* LastItem = NULL;//This would point to the last item of the list
public:
template<class T>
void AddItemToList(T& Item) {
if(LastItem) {
Iterator* it = new Iterator{&Item, NULL, LastItem};
LastItem->next = it;
LastItem = it;
} else {
Iterator* it = new Iterator{&Item, NULL, NULL};
FirstItem = it;
LastItem = it;
}
}
template<class T>
void RemoveItemFromList(const T& Item) { 
for(Iterator* it = FirstItem; it != NULL; it = it->next) {
if(it->item == &Item) {
if(it->prev)
it->prev->next = it->next;
else
FirstItem = it->next;
if(it->next)
it->next->pref = it->pref;
else
LastItem = it->pref;
delete it;
break;
}
}
}
};

未测试代码