本技术相对来说比较难理解,这种技术,是在stl源码中使用到的,
给我感觉的作用就是对类的解耦。
因为,工作中用到这种技术相对来说,比较少,所以,我目前还不是非常理解,下面是我敲书上的例子。
首先上一段代码
#ifndef BEARCORNER_H
#define BEARCORNER_H
#include <iostream>
using std::ostream;
class Milk
{
public:
friend ostream& operator<<(ostream& os,const Milk&)
{
return os<<"Milk";
}
};
class CondensedMilk
{
public:
friend ostream& operator<<(ostream& os,const CondensedMilk&)
{
return os<<"Milk";
}
};
class Honey
{
public:
friend ostream&
operator <<(ostream& os,const Honey&)
{
return os<<"Honey";
}
};
class Cookies {
friend ostream&
operator <<(ostream& os,const Cookies&)
{
return os<<"Cookies";
}
};
class Bear {
friend ostream&
operator <<(ostream& os,const Bear&)
{
return os<<"Theodore";
}
};
class Boy {
friend ostream&
operator <<(ostream& os,const Boy&)
{
return os<<"Patrick";
}
};
template<class Guest> class GuestTraits;
template<> class GuestTraits<Bear>
{
public:
typedef CondensedMilk beverage_type;
typedef Honey snack_type;
};
template<> class GuestTraits<Boy>
{
public:
typedef Milk beverage_type;
typedef Cookies snack_type;
};
#endif
// BearCorner.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "BearCorner.h"
#include <iostream>
using namespace std;
class MixedUpTraits
{
public:
typedef Milk beverage_type;
typedef Honey snack_type;
};
template<class Guest,class traits= GuestTraits<Guest> >
class BearCorner{
Guest theGuest;
typedef typename traits::beverage_type beverage_type;
typedef typename traits::snack_type snack_type;
beverage_type bev;
snack_type snack;
public:
BearCorner(const Guest& g):theGuest(g){
}
void entertain(){
cout<< "Entertaining "<<theGuest<<" serving "
<<bev <<" and "<<snack <<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Boy cr;
BearCorner<Boy> pc1(cr);
pc1.entertain();
Bear pb;
BearCorner<Bear> pc2(pb);
pc2.entertain();
BearCorner<Bear,MixedUpTraits> pc3(pb);
pc3.entertain();
return 0;
}
</pre><pre name="code" class="cpp">
</pre></p><p></p><p>其中 typename 中就是告诉这个东西是一个类型。否则是编译不过去的。编译器不知道那个是什么</p><p></p><p>从类中可以看出,用户类和食品中,通一个特化魔板,也就是提供的的traites层。</p><p>当然类中也提供了一个简单的复合特征类需求的类来代替他。</p><p></p><p>结果入输出如下:</p><p><img src="https://img-blog.csdn.net/20151128011110488?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p></p><p></p><p><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">更多文章,欢迎访问http:</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">//blog.csdn.net/wallwind</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">我的代码目录:http:</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">//code.taobao.org/u/wallwind/mypro/</span></p><p></p><div style="top: 649px;"><pre name="code" class="cpp">// BearCorner.cpp : 定义控制台
</pre><pre name="code" class="cpp">