87型雨水斗和65雨水斗:[c primer plus]c 函数模版,类模版,实例化,具体化,模版作参数,模...
来源:百度文库 编辑:偶看新闻 时间:2024/03/29 16:13:44
[c++ primer plus]c++ 函数模版,类模版,实例化,具体化,模版作参数,模版的友元 收藏
模版不是类或函数,它们是c++编译器指令,它只是一个用于生成类或函数的方案,说明如何生成类或函数。 具体生成时,称为实例化(instantiation)或具体化(specialization)。因此,模版不能单独编译,必须与特定的实例化请求一起使用。最简单的办法就是将所有模版信息放入一个头文件里,使用时include这个头文件。如果编译器支持export关键词,则可以将“类模版”的声明与定义分开存放在.h和.cpp中。
1. 函数模版的重载(利用可以重载的特征标)
template
void Swap(Any& a, Any& b);
//override
template
void Swap(Any* a, Any* b, int n);
template
void Swap(Any& a, Any& b)
...{
Any temp; temp = a; a = b; b = temp;
}
template
void Swap(Any* a, Any* b, int n)
...{
Any temp;
for (int i = 0; i < n; i++)
...{ temp = a[i]; a[i] = b[i]; b[i] = temp; }
}
main( )
{
int i, j;
double k[10], m[10];
Swap(i, j); //显示实例化为Swap
Swap(k, m, 10); //显示实例化为Swap
}
2. 函数模版的显式实例化,即可以命令编译器直接创建 template
//explicit instantiation
template void Swap
template void Swap
3. 函数模版的显式具体化,它不使用模版来生成函数定义,而应使用独立的、专门的函数定义。显式具体化要有自己的函数定义,而显式实例化则不需要,这是两者本质的区别。两者的相同之处是:都生成了一个具体的函数,而不再是模板。
// explicit specialization,这两种格式显式实例化的格式等价,只需要一种就可以。
template <> void Swap
template <> void Swap(Cjob&a, Cjob& b);
template <> void Swap
...{
string tStr;
tStr = a.name;
a.name = b.name;
b.name = tStr;
}
template <> void Swap(Cjob& a, Cjob& b)
...{
string tStr;
tStr = a.name;
a.name = b.name;
b.name = tStr;
}
4. 函数模版的部分具体化,部分具体化与显式具体化的区别在于:前者还是一个函数模板,而后者则已经是一个函数。前者的函数定义中,必然还有typename类型待定,这个类型将在函数模板被调用的时候得到“隐式”的具体化。
template
void Swap(T1& a, T2& b, T3& c);
//explicit specialization
template
//部分具体化,可以引入各种限制
template
tempate
template
void Swap(T1& a, T2& b, T3& c)
...{
..............
}
template
...{
..............
}
template
...{
.............
}
template
...{
..............
}
5. 类模版之内与之外:在之内声明时可以忽略类型,之外必须Stack
template
class Stack
...{
public:
Stack& operator= (const Stack& st);
}
template
Stack
...{
}
main ( )
{
Stack
}
6. 类模版的默认类型参数,不能为函数模版提供默认的类型参数。
template
class Topo
...{
........
};
Topo
Topo
7a. 类模板的显式实例化
template
class SortedArray
...{
public:
T val
};
template class SortedArray
main ( )
...{
SortedArray
}
7b. 类模板的显式具体化
template
class SortedArray
...{
........
};
//显式具体化,定义新的类功能。
template <> class SortedArray
...{
........
};
main ( )
{
SortedArray
}
7c. 类模版的部分具体化
template
class SortedArray
...{
public:
SortedArray()
...{
printf("1 ");
}
T1 val1;
T2 val2;
T3 val3;
};
template
class SortedArray
...{
public:
SortedArray()
...{
printf("2 ");
}
T1 val1;
T2 val2;
};
template
class SortedArray
...{
public:
SortedArray()
...{
printf("3 ");
}
T1 val;
};
template <>
class SortedArray
...{
public:
SortedArray()
...{
printf("4 ");
}
};
int main(int argc, char* argv[])
...{
SortedArray
SortedArray
SortedArray
//SortedArray sa4;
printf("Hello World! ");
return 0;
}
8. 模版作为模版类的成员
template
class beta
...{
private:
template
class hold;
hold
hold
public:
template
U blab(U u, T t);
};
//member define
template
template
class beta
...{
private:
V val;
public:
V Value() const ...{return val;}
};
template
template
U beta
...{
........
};
9. 模版作为模版的参数,vc++不支持这种使用,g++支持。
template
class Stack
...{
....
};
template class Thing>
class Crab
...{
....
};
main()
...{
Crab
}
10. 模版类的非模版友元函数
template
class HasFriend
...{
public:
HasFriend(const T& i) : item(i) ...{ ct++; }
~HasFriend() ...{ ct--; }
friend void counts();
friend void report(HasFriend
private:
T item;
static int ct;
};
void counts()
...{
// 每个类有独立的static变量,模版没有static变量,因为模版只是提供给编译器的方案,本身并不是类。只有模版实例化或具体化之后,才可以访问其static变量。
cout << "HasFriend
cout << "HasFriend
}
// 必须为每个类提供相应的友元。
void report(HasFriend
...{
cout << hf.itme;
}
void report(HasFriend
...{
cout << hf.item;
}
main()
...{
HasFriend
counts();
HasFriend
counts();
report(hf2);
}
10. 模版类的约束模版友元。
//首先在类定义前面声明-函数是模版函数
template
template
template
class HasFriendT
...{
public:
HasFriendT(const TT& i) : item(i) ...{ ct++; }
~HasFriendT() ...{ ct--; }
//在类里再次声明为友元,这里也是某种程度上的实例化,
//但还没有彻底实例化,函数还是模版。
friend void counts ();
friend void reports
//这里也可以写做:
//friend void reports<> (HasFriendT&);
//让编译器自己判断模版的参数类型!
private:
TT item;
static int ct;
};
template
int HasFriendT
template
void counts()
...{
//这里并不是要输出模版的大小、模版的static成员变量。因为程序运行时,并不存在模版的实体,
//都是实例化后的确确实实存在的类。因此,这里指的实例化的类。
cout << "template size: " << sizeof(HasFriendT
cout << "template counts(): " << HasFriendT
}
template
void report(T& hf)
...{
cout << hf.item << endl;
}
int main()
...{
counts
HasFriendT
HasFriendT
HasFriendT
report(hfi1); //通过调用,隐式实例化函数
report(hfi2);
report(hfdb);
return 1;
}
11. 模版类的非约束模版友元。
template
class ManyFriend
...{
public:
ManyFriend(const T& i) : item(i) ...{}
template
private:
T item;
};
template
void show2(C& c, D& d)
...{
cout << c.item << ", " << d.item << endl;
}
int main()
...{
ManyFriend
ManyFriend
ManyFriend
cout << "hfi1, hfi2: ";
show2(hfi1, hfi2);
cout << "hfdb, hfi2: ";
show2(hfdb, hfi2);
return 0;
}
发表于 @ 2009年04月21日 20:44:00 | 评论( 0 ) | 编辑| 举报| 收藏
旧一篇:禁止类继承的方法 | 新一篇: linux-socket详解与实例给billpig的留言只有注册用户才能发表评论!登录注册姓 名:
校验码:
Copyright ? billpig
Powered by CSDN Blog
近期热点文章推荐 近期热点文章推荐1、程序优化利器:ATOM 2、简单有效快速定位内存错误3、成功案例,免费下载!4、AMT视频解说——通过WebUI管理AMT客户机5、从Embedded说起6、(图)集成显卡硬件解码7、免费开源应用程序下载多核、游戏、高性能,尽在英特尔? 软件网络
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/billpig/archive/2009/04/21/4098655.aspx