在链接列表中使用C ++模板,列表中出现多种不同的类型 [英] Using C++ templates in Linked Lists, with multiple different types appearing in the list

查看:24
本文介绍了在链接列表中使用C ++模板,列表中出现多种不同的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(首先,作为免责声明,这与一项作业有关.我并不是要任何人为我做作业,只是想帮助我了解如何正确实现模板.)

(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone to do my assignment for me, just to try and help me understand how to implement templates properly.)

我当前的设置是:

我有A类,这是基类.B,C和D类是A类的所有子级.

I have class A, which is a base class. Class B, C and D all children of Class A.

我正在尝试创建一个链接列表,该链接列表可以在一个列表中指向B,C或D.

I'm trying to make a linked list that, within a single list, can point to either B, C or D.

我目前的设置是这样的:

What I currently have setup is something like this:

enum Types { TypeB, TypeC, TypeD }

struct Node
{
    void * pointerToElement;
    int type;
    Node * next;
};

struct Header
{
    int counter;
    Node * first;
};

这实际上有效.当我遍历链表打印出所有元素时,我使用and if语句和int type来识别它是什么类型(基于定义的ENUM),然后使用 static_cast 将void指针转换为B,C或D类的指针.

This actually works. When I go through the linked list to print out all the elements, I use the and if statement and the int type to identify what kind it is (based on the ENUM defined), and then use static_cast to cast the void pointer to a pointer of either class B, C or D.

现在,有人告诉我必须使用模板来代替,这引起了很多麻烦.我对模板没有做太多事情,但是我对模板的体验并不尽如人意.

Now, I've been told I'm required to use templates instead, which is causing a whole lot of headache. I haven't done much with templates, but my experience with them hasn't been all that pleasant.

我对模板的理解是,我可以使用它来定义EITHER类B,C或D的整个链表,但是将B,C或D都出现在同一个链表中似乎不合理吗?

My understanding of templates is that I could use it to define the whole linked list with EITHER class B, C or D, but it wouldn't be plausible to have B, C or D all appearing in the same linked list?

我尝试了以下操作:

enum Types { TypeB, TypeC, TypeD } // I realise that if templates work, I won't need this

template <class T>
struct Node
{
    T * pointerToElement;
    int type;
    Node<T> * next;    // Reason 1 I suspect I could only use one type
};

template <class T>
struct Header
{
    int counter;
    Node<T> * first;    // Reason 2 I suspect I could only use one type
};

我的主要问题是,模板应该能够做到这一点吗?当在类中实现它时,我需要为报头指定一种类型,而这是我不想做的,因此我也将该类作为模板,并且它一直跟踪我其余的代码,需要成为一个模板,最后到达main(),在那我必须定义B,C或D类.

The main question I have, are templates supposed to be able to do this? When implementing it in a class, I needed to specify a type for the header, which I didn't want to do, so I made that class a template as well, and it kept following through to the rest of my code which shouldn't need to be a template, and finally got to main() where I would have had to define either class B, C or D.

感谢评论和建议.

谢谢.

修改

感谢大家的评论,然后我可能会从讲座中学到更多尝试方法.

Thanks everyone for the comments, I've probably learnt more trying this then from the lectures.

我所做的几乎是沟渠的模板,或者至少是我尝试使用它们的方式.我已经使用了模板(不幸的是,为了使用模板,它可以工作).这就是我现在所做的(所有有用的注释中都得出了结论……谢谢!)

What I've done is pretty much ditched templates, or at least the way I was trying to use them. I have used templates (unfortunately for the sake of using templates,) and it works. Here's what I've now done (all worked out from all of the helpful comments... thanks!)

template <class T>
struct Node
{
    T * pointerToElement;
    int type;   // I can get rid of this after I go through the code and remove all references to it, which I am doing now.
    Node<T> * next;    // Reason 1 I suspect I could only use one type
};

template <class T>
struct Header
{
    int counter;
    Node<T> * first;    // Reason 2 I suspect I could only use one type
};

仍然是原来的样子,但是在声明 Header 时,我将其声明为:

is still the way it was, but when declaring the Header, I declare it as:

Header * myHeader;

Header * myHeader;

(我使用的类结构与以下解决方案中的类结构相同).

(I'm using a class structure identical to the one in the solution below).

因此,这指向基类,所有其他类都派生自该基类.然后,由于继承,我可以毫无问题地将类B,C或D存储在其中,并且假设派生类(B,C和D)中定义的所有函数都在基类中定义,则可以将其称为直接进行而不需要强制转换(例如,它们都有自己的打印函数,并且在派生类中定义了正确的函数).

So this is pointing to the base class, the one that all the other classes are derived from. Then, because of inheritance, I can store class B, C or D in there without any problems, and providing that all the functions defined in the derived classes (B, C and D) are defined in the base class, I can call it directly without having to cast it (for example, they all have their own print function, and it calls the correct one when it is defined in the derived class).

我认为,Assignment试图克服的想法是可以使用任何类型的链表,我认为如果应该使用模板,可能会出现一些沟通不畅(可能主要是由于我的缘故).在每个节点中定义不同的类类型,而是可以使用它们来定义基类.

I think the idea that the Assignment is trying to get across is a linked list that can be used with any type, I think there was some miscommunication (probably mostly due to me,) were I thought templates were supposed to be used to define different class types in each node, rather they can be used to define the base class.

推荐答案

您应该使用继承而不是模板.正如您所猜想的那样,模板会按类型 T 创建实例,而这并不是您想要的.

Instead of templates you should be using inheritance. Templates, as you guessed, create instance per type T, and that is not what you want.

您的代码应在以下几行中显示:

Your code should be something in these lines:

class A{...};
class B: public A{...};
class C: public A{...};
class D: public A{...};
struct Node{
    A *next;
}

您可以将 next 指针分配给 A B C D.确保在适当的地方将成员函数标记为 virtual .

you can assign to next pointers to either A, B, C, or D. Make sure to mark member functions as virtual where appropriate.

这篇关于在链接列表中使用C ++模板,列表中出现多种不同的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆