C++ 模板实现动态开辟结构体怎么写

来源:百度知道 编辑:UC知道 时间:2024/07/04 18:13:23
struct memerytype
{
char parttern[20];
int price;
int num;
struct memerytype *next;
};
struct monitertype
{
char parttern[20];
int price;
int num;
struct monitertype *next;
};
在开辟这两个结构体时我想用同一个模板要咋的办捏?
小鸟刚入门,谢谢能不能具体点。
就是用链表来存放这些个呀,
就是不懂怎么写怎么用模板嘛,
帮我写一个模板用来开辟动态存储空间,存入这些个

做不到哦, 必须类型要一致才可以。但是也有变通的方法,就是定义一个基类,上面2个类继承之,使用多态性实现。

struct BaseStruct
{
char parttern[20];
int price;
int num;
}

struct memerytype:public BaseStruct
{
struct memerytype *next;
}

struct monitertype:public BaseStruct
{
struct monitertype *next;
}

然后\
#include <list>
using namespace std;

list<BaseStruct*> pList;

"开辟"?这是什么术语?你的代码里根本没有什么模板啊!