C++笔记(类模版)

//模版类中有模版函数偏特化, 有模版泛化, 全特化, 重载
//模版函数调用优先级: 全特化, 特化, 泛化

//泛化
template <typename T,typename U>
struct TC
{
	TC()
	{
		cout << "TC泛化版本构造函数" << endl;
	}
	void functest1()
	{
		cout << "functest1泛化版本" << endl;
	}

	static int m_stc;  //声明一个静态成员变量
};
template <typename T, typename U>
int TC<T,U>::m_stc = 50; //定义静态成员变量, 偏特化

//template <>
int TC<double, int>::m_stc = 100;//定义为全特化


    template <> //全特化:所有类型模板参数都yoghurt具体类型代表,所以<>里就空了
	struct TC<int, int>
	{
		TC()
		{
			cout << "TC<int,int>特化版本构造函数" << endl;
		}
		void functest1();
		{
			cout << "functest1特化版本" << endl;
		}

		void functest2();
		{
			cout << "functest2特化版本" << endl;
		}
	};



//-------------------------
	template <typename U>
	struct TC<float, U>
	{
		TC()
		{
			cout << "TC<float,U>偏特化版本构造函数" << endl;
		}
		void functest1();
	};
	template <typename U>
	void  TC<float, U>::functest1()
	{
		cout << "TC<float,U>::functest1偏特化版本" << endl;
	}

	//-------------------------
	template <typename T, typename U>
	struct TC<const T, U*>
	{
		TC()
		{
			cout << "TC<const T,U*>偏特化版本构造函数" << endl;
		}
		void functest1();
	};
	template <typename T, typename U>
	void TC<const T, U*>::functest1()
	{
		cout << "TC<const T,U*>::functest1偏特化版本" << endl;
	}
///////////////////////////////////////////////////////////////////////////////
//缺省参数(只能在泛化版本中才能有, 特化版本是没有的)
template <typename T,typename U = int>
struct TC
{
	TC()
	{
		cout << "TC泛化版本构造函数" << endl;
	}
	void functest1()
	{
		cout << "functest1泛化版本" << endl;
	}

	static int m_stc;  //声明一个静态成员变量
};
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值