创建对象的链接列表
我有一个类作业(阅读:不使用 STL),我需要在其中创建一个排序的对象链接列表,但我不太确定如何执行此操作。
我使用的类同时包含整数和字符串成员,但只会对这些整数成员之一进行排序。我目前有一个功能齐全的链表模板,可以使用整数数据成功运行。
现在我的问题在于将其转换为适用于我的班级。实例化此链表时,必须定义
,在本例中,类型为 Poster
,位于我正在排序的 Poster 类之后。但是,在链表类的声明中,有一个 class Node
的声明和定义,其中写着:
class Node
{
public:
Type Element;
Node *Next, *Previous;
Node() : Next(NULL), Previous(NULL) {} // Default constructor
Node (Type Data, Node *PNode = NULL) : // Non-default constructor
Element (Data),
Next (PNode),
Previous (PNode) {}
};
我不确定当 Poster
的成员时,此现有定义将如何工作code> 在 LinkedList
Node
定义替换为 class Poster
的内容,或者将节点中的 Type Element
标记为某种 catch -class Poster
成员的所有容器,以便可以通过 Element.GetMemberValue()
访问 Poster
的成员?
I have a class assignment (read: no use of the STL) where I need to create a sorted linked list of objects, but I'm not quite sure how to go about doing this.
The class that I am using contains both integer and string members, but it is only one of these integer members that will be sorted. I currently have a fully functioning linked list template that will successfully run with integer data.
Now my problem lies in converting this to work with my class. When instantiating this linked list, a <Type>
must be defined, in this case that type is Poster
, after the class Poster that I am sorting. However, in the declaration of the linked list class, there is a declaration and definition of the class Node
which reads
class Node
{
public:
Type Element;
Node *Next, *Previous;
Node() : Next(NULL), Previous(NULL) {} // Default constructor
Node (Type Data, Node *PNode = NULL) : // Non-default constructor
Element (Data),
Next (PNode),
Previous (PNode) {}
};
I'm unsure how this existing definition will work when the members of Poster
are introduced when LinkedList<Poster> listOfPosters
is declared. Should I replace the above definition of Node
with the contents of class Poster
, or will Type Element
in the node be marked as a sort of catch-all container for the members of class Poster
, so that members of Poster
can be accessible via Element.GetMemberValue()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测 LinkedList 类的声明看起来像
当使用链表类(主程序)的程序实例化一个 LinkedList 类
时,如下所示 编译器在编译时从模板生成代码,并替换所有出现的“类型”与“海报”。因此,您不需要更改链表或节点类。
Im guessing that the declaration of the LinkedList class looks something like
When the program that uses the linked list class (the main program) instantiates a LinkedList class like following
The compiler, at compile time, generates code from the template, and replaces all occurrences of "Type" with "Poster". Therefore, you dont need to change the linked-list or node class.
Node.Element
成员将存储Poster
的(按值)副本。无需手动替换 Node 定义中的任何内容;这就是 C++ 模板的全部意义。当您声明
LinkedList
对象时,编译器将生成一个新类,并用Poster
替换Type
占位符的所有实例。这将在LinkedList
类中自动创建一个适当的Node
类,该类表示保存Poster
对象的节点。The
Node.Element
member will store a (by-value) copy of thePoster
. There is no need to manually replace anything in the definition of Node; that's the whole point of C++ templates.When you declare a
LinkedList<Poster>
object, the compiler will generate a new class and substitute all instances of theType
placeholder withPoster
. This will automatically create an appropriateNode
class inside theLinkedList<Poster>
class that represents nodes which hold aPoster
object.