VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Generic Programming

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

Hello all. It's nice to meet you all here. I got a lot of knowledge from csdn. now, I try to feed back some. the topic is about Generic Programming.

I paste a snippet to show how it works.
Everyone who has studied the book Think in C++ 2nd Edition knows the sample, trash collection, which is at the end of chapter 10, Design pattern.
Ok, let's begin with this.
the sample mentioned in Think in C++ using double dispatch to implement the design. and I use Generic Programming method to do the same thing.
Enjoy.

#include<vector>
#include <iostream>
using namespace std;
class Trash
{
};
class Paperbin;
class Glassbin;
class Paper:public Trash
{
public:
typedef Paperbin aBin;
};

class Glass:public Trash
{
public:
typedef Glassbin aBin;
};

class Paperbin:public vector<Paper>
{
};
class Glassbin:public vector<Glass>
{
};

template<int v>
struct Int2Type
{
enum{value=v};
};
template<class T, class U>
class Conversion
{
typedef char Small;
struct Big {char dummy[2];
};
static Small Test(U);
static Big Test(...);
static T MakeT();
public:
enum{ exists=(sizeof(Test(MakeT()))==sizeof(Small))};
};
template<class Trashs, class Bin>
Add(Trashs trash, Bin bin)
{
cout<<Conversion<Trashs::aBin ,Bin>::exists<<endl;
_Add(trash,bin,Int2Type<Conversion<Trashs::aBin ,Bin>::exists >());
}

template<class Trash, class Bin>
_Add(Trash trash, Bin bin,Int2Type<true>)
{
bin.push_back(trash);
cout<<"success adding"<<endl;
};
template<class Trash, class Bin>
_Add(Trash trash, Bin bin,Int2Type<false>)
{
cout<<"failed adding"<<endl;
};

int main()
{

Paper apaper;
Glass aglass;
Paperbin apaperbin;
Glassbin aglassbin;
Add(apaper,apaperbin);
Add(aglass,aglassbin);
Add(apaper,aglassbin);
Add(aglass,apaperbin);
return 0;
}

you can reach me by send me a email at [email protected].



相关文章

相关软件