#include
template
struct Map
{
typedef T value;
typedef T& reference;
typedef T* pointer;
};
void test1()
{
Map::value a = 100.9; //a的类型是int 所以是100
std::cout << a << std::endl;
Map::value b = 100.9; //同理 是100.9
std::cout << b << std::endl;
}
/**************************
如果一个类模板中,全部的成员都是共有的数据类型,
这个模板就是一个独立的数据类型表,用来规范数据
第一步:我们定义一个规范类模板类型表的基类模板
第二部:凡是继承了这个类型表的模板,它的访问类型就被确定了
***********************************/
template
class Typeb1
{
public:
typedef T value_type1;
typedef T& reference1;
typedef U value_type2;
typedef U& reference2;
};
/**************************
在STL库中,设计人员经常使用这种技巧
binary,对二元函数的形参进行了约束
template
struct binary {
typedef _A1 Arg1;//第一个形参类型
typedef _A2 Arg2;//第二个形参类型
typedef _R Rtn;//返回值类型
};
***********************************/
template
class A :public Typeb1
{
};
void test2()
{
A::value_type1 a = 100;
A::reference1 b = a;
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
}
template
struct Binary
{
typedef _A1 Arg1;
typedef _A2 Arg2;
typedef _R Rtn;
};
/**************************
因为Add包含了binnary 的数据类型,因为系统中其他模块就可以使用
Add::Arg1 A::Arg2这种方式和Add本身进行对接
这种数据类型的抽象:达到了多个系统模块之间的类型统一。
***********************************/
template
class Add :public Binary
{
public:
TR bFunction(const T1& x, const T2& y) const {
return x + y;
}
};
void test3()
{
double a = 100.1, b = 20;
//AddaddObj;
typename AddaddObj;
std::cout << addObj.bFunction(a,b) << std::endl;
//typename Add::Arg1 x = 1000; 同下
Add::Arg1 x = 1000;
std::cout << x << std::endl;
}
int main(void)
{
//test1();
//test2();
test3();
return 0;
}
example2
#include
class Test1
{
public:
char change(int x, double y) {
return x;
}
};
class Test2
{
public:
double change(double x, double y) {
return x;
}
};
template
/**************************************
侵入了test1 和test2
***************************************/
class Test
{
public:
Rt change(T1 t1, T2 t2) {
return t1;
}
};
/**************************************
两个类模板规范一个统一的接口
***************************************/
class TestTb1;
class TestTb2;
template class TypeTb{};
template<>
class TypeTb
{
public:
typedef char ret_type;
typedef int par1_type;
typedef double par2_type;
};
template<>
class TypeTb
{
public:
typedef char ret_tyep;
typedef double par1_type;
typedef double par2_type;
};
template
class TypeTbt
{
public:
typename TypeTb::ret_type change(typename TypeTb::par1_type x, \
typename TypeTb::par1_type y) {
return x;
}
};
int main()
{
Test tt;
std::cout << tt.change(67, 22.0) << std::endl;
TypeTbt tt1;
std::cout << tt1.change(68, 22.0) << std::endl;
return 0;
}
example3
#include
template
class Iterator1
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
};
template
class Iterator2
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
};
template struct Traits{};
template
class Traits
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
};
int main()
{
Iterator1 ::value_type t1 = 100;
Iterator1::value_type t2 = 3.14;
Traits::value_type t3 = 1.11;
std::cout << t1 << std::endl << t2 << std::endl << t3 << std::endl;
return 0;
}
欢迎分享,转载请注明来源:内存溢出
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
example 1
评论列表(0条)