深入模板编程笔记二

//链表
template<typename t, typename n>
struct hetero_node {//effective stl说过,访问数据永远比访问方法快,所以尽量用struct
	t value;
	n* next;
	hetero_node(t const& v, n* n1): value(v), next(n) {}
};

typedef hetero_node<int, void> node_0;
node_0* p0;
typedef hetero_node<char, node_0> node_1;
node_1* p1;
typedef hetero_node<std::string, node_1> node_2;
node_2* p2;

void main()
{
	p1->next = p0;
	p2->next = p1;
	//很佩服这样的思路,用模板封装基链表,然后自己扩展链接。
	system("pause");
}



//静态变量
the_class_1.h
template<typename t>
struct the_class {
	static int id;
	the_class() {id++;}
};
template<typename t> int the_class<t>::id = 1;//注意

the_class_2.h
template<typename t>
struct the_class {
	static int id;
	the_class() {id++;}
};
template<typename t> int the_class<t>::id = 0;//注意

//call1.cpp
#include "the_class1.h"
void call1() {
	the_class<int> c;
	std::cout<<c.id;
}
//call2.cpp
#include "the_class2.h"
void call2() {
	the_class<int> c;
	std::cout << c.id;
}
//main.cpp
void call1();
void call2();
void main()
{
	call1();
	call2();
	system("pause");
}//测试后,发现访问了同一个模板函数,访问了同一个静态成员变量。     也就是说永远以第一次,识别创建的对象为主。前篇理论一样。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值