随笔 - 455  文章 - 694  trackbacks - 0
<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234


子曾经曰过:编程无他,唯手熟尔!
feedsky
抓虾
pageflakes
Rojo
狗狗
google reader
bloglines
my yahoo
newsgator
netvibes
鲜果


Locations of visitors to this page

常用链接

留言簿(50)

随笔分类(591)

随笔档案(455)

相册

BCB

Game Industry

OGRE

other

Programmers

Qt

special guys

  • bright tomorrow
  • A guy who has very close blood relationship with me!
  • 俺是山东人啊
  • One guy who often makes me cry in silent night.I love him so much, but I cannot see him any more, cause the God wanted him to be around.

WOW Stuff

搜索

  •  

积分与排名

  • 积分 - 202621
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜

60天内阅读排行

使用boost::any实现virtual template成员函数

如你所知,C++中没有提供virtual template function。然而有时候你的确会有这种需要,any可以一定程度上满足这种需要,例如,

						
								
1 class Base
 2 {
 3 public:
 4         virtual void Accept(boost::any anyData)
 5         {
 6                 
 7         }
 8 };
 9 class Derived:public Base
10 {
public:
11         virtual void Accept(boost::any anyData)
12         {
13                 
14         }
15 };
这样的Accept函数能够接受任意类型的数据,并且是virtual函数
//==================

俺是没这种需求,不过看到了这个,觉得好玩。

Q: c++中为什么没有提供virtual template function?
A: 因为用不同的 template parameters(模板参数)实例化 function templates(函数模板)导致不同的函数被调用,这   是发生在编译期的, 以 compile-time polymorphism(编译期多态)著称。
   而virtual member funtion是通过dynamic binding(运行期)来实现不同的函数被调用,是runtime polymorphism.
  
posted on 2006-10-30 23:00 七星重剑 阅读(754) 评论(4)  编辑 收藏 引用 所属分类: PL--c/c++

FeedBack:
# re: [推荐]用boost::any实现virtual template function 2006-10-30 23:10 阿来
msdn 只是说: 成员函数模板不能是虚拟的

但是 why?

哪位高手给解释一下?  回复  更多评论
  
# re: [推荐]用boost::any实现virtual template function 2006-10-30 23:33 阿来
自己解释了下,不够深入  回复  更多评论
  
# re: [推荐]用boost::any实现virtual template function 2006-11-01 08:21 shaovie
one compile-time polymorphism, one run-time dynamic binding. can't be declared the same time  回复  更多评论
  
# re: [推荐]用boost::any实现virtual template function 2006-11-01 11:16 阿来
@shaovie
Damn right!  回复  更多评论
  

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]

相关链接:
网站导航: