//链表
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");
}//测试后,发现访问了同一个模板函数,访问了同一个静态成员变量。 也就是说永远以第一次,识别创建的对象为主。前篇理论一样。