类模板与特化

时间:2022-05-25 20:51:24

直接上代码,详见代码中注释:


/*
 * list.h
 *
 *  Created on: 
 *      Author: wdmcel
 */

#ifndef _LIST_H_
#define _LIST_H_

#include <string.h>
#include <iostream>
using namespace std;

class test_t                       //定义了一个用于测试模板特化的类
{
public:

	test_t(int x_ = 0, int y_ = 0)        //构造函数,由于形参提供了默认值,无须重载test_t()
	{
		m_x = x_;
		m_y = y_;
	}

	~ test_t()
	{
	}

	test_t (const test_t& test_)         //复制构造函数,形参必须为引用,否则会递归调用至栈溢出
	{
		m_x = test_.m_x;
		m_y = test_.m_y;

	}

	test_t& operator=(const test_t& test_)     //重载赋值运算符,返回引用合保证能够连用“=”
	{
		m_x = test_.m_x;
		m_y = test_.m_y;
		return *this;
	}

	int get_x()
	{
		return m_x;
	}

	int get_y()
	{
		return m_y;
	}

private:                 //含两个成员变量
	int m_x;
	int m_y;
};

template <typename T>               //定义模板类,包含一个数据成员data,及一个指向该类实例的指针
struct node_t
{
	node_t(T data_) : next(NULL)
	{
		data = data_;
	}

	T data;
	node_t* next;
};
template <>
struct node_t<char*>                  //模板参数特化,char*不能如同普通类型数据一样处理,因此需要特化
{
	node_t(char* data_ = NULL) : next(NULL)
		{
		    data = new char[strlen(data_) + 1];
		    if (NULL != data)
		    {
		    	memcpy(data, data_, strlen(data_));
		    	data[strlen(data_)] = '\0';
		    }
		}
	//第100行删除节点的时候,已经delete了
	~node_t()
	{
		if (NULL != data)
		{
			delete data;
			data = NULL;
		}
	}

	char* data;
	node_t* next;
};
template <>
struct node_t<test_t>              //对test_t类进行特化
{
	node_t(const test_t& test_) : next(NULL)
    {
	    data = test_;
    }

	test_t data;
	node_t *next;
};

template <typename C>
class list_t                     //实现list类
{
public:
	list_t(C data_): m_mount(0)
	{
		m_list = new node_t<C>(data_);
	}
	~list_t()
	{
		if (NULL != m_list)
		{
			delete m_list;
			m_list = NULL;
		}
	}

	int get_mount()
	{
		return m_mount;
	}

	node_t<C>* get_list()
	{
		return m_list;
	}

	void insert_list(C& data_)
	{
		node_t<C> *tmp;

		tmp = new node_t<C>(data_);

		tmp->data = data_;

		tmp->next = m_list->next;

		m_list->next = tmp;

		m_mount++;
	}

	node_t<C> * delete_list(node_t<C> * node_)
	{
		//cout << "delete data : " << node_->data << endl;

		m_list->next = node_->next;

		delete node_;

		node_ = m_list->next;

		m_mount--;

		return node_;
	}


	void show_list()
	{
		node_t<C> *tmp;

		tmp = m_list->next;

		while (NULL != tmp)
		{
			cout << tmp->data << endl;
			tmp = tmp->next;
		}
	}

private:

	int m_mount;
	node_t<C>  * m_list;
};


#endif /* LIST_H_ */
测试代码:

#include "list.h"

int main()
{
/*	list_t<double> list(1.0);
	double data_double = 1.234;
	for (int i = 0; i <= 10; i++)
	{
		list.insert_list(data_double);
	}
	node_t<double> *tmp;
	tmp = list.get_list() -> next;
	while (NULL != (tmp = list.delete_list(tmp)))
	{
	}
*/

/*	list_t<string> list("");
	string data_string("wdmcel");
	for (int i = 0; i <= 10; i++)
	{
		list.insert_list(data_string);
	}
	node_t<string> *tmp;
	tmp = list.get_list() -> next;
	while (NULL != (tmp = list.delete_list(tmp)))
	{

	}
*/

/*	list_t<char*> list("");
	char* data_pchar = ("wdmyong");
	for (int i = 0; i <= 10; i++)
	{
		list.insert_list(data_pchar);
	}
	node_t<char*> *tmp;
	tmp = list.get_list() -> next;

	while (NULL != (tmp = list.delete_list(tmp)))
	{
	}
*/
	test_t data_class(2,4);
	list_t<test_t> list(data_class);
	for (int i = 0; i <= 10; i++)
	{
		list.insert_list(data_class);
	}
	node_t<test_t> *tmp;
	tmp = list.get_list() -> next;

	while (NULL != (tmp = list.delete_list(tmp)))
	{
		//cout << tmp->data.get_x() << endl;
	}
	system("pause");
	return 0;
}
双向链表下次奉上~