力为的技术博客

联系 聚合 管理
  154 Posts :: 1 Stories :: 561 Comments :: 0 Trackbacks

An Adapter Solution

Long long ago, there were one couple who lived in a very mystical village. They had two children,but their children couldn't speak. The worst thing was that nobody knew they were girl or they were boy, even the couple!

 

class  Father
{
}


class  Mother
{
}


class  Child1 :  public  Father,  public  Mother
{
}


class  Child2 :  public  Father,  public  Mother
{
}



Then one day, an old man, from very far away, nobody knew where he came from and what he would to do, came to the village. It was very strange that the whole village became exciting after he stayed there only for a day. That's because he found the sex of the couple's children, and he also made the two little to speak, because he had magic tools:

 

enum  SEX
{
 GRIL,
 BOY
}
;

class  NewFound
{
public :
 
void  Speak() {}

}


template
< class  P,  class  N, SEX sex >
class  ChildT :  public  P,  public  N
{
public :
  SEX getSex()
{ return  sex;}
}

 

So the two children look like as the following:

 

typedef ChildT < Child1, NewFound, BOX >  Son;
typedef ChildT
< Child2, NewFound, GIRL >  Girl; 

Son son; 
SEX sex 
=  son.getSex();
son.Speak();

Girl girl;
sex 
=  girl.getSex();
gril.Speak();

 

As time gone by, the couple had another girl. The first day of her life, she learned to speak, and the second day, she learned the ability to fly!!! What a strange girl! You may ask whether she was an angel. Maybe only the god knows that.

 

class  NewGirl :  public  ChildT < Father, NewAblity, GIRL > ,  public  Mother
{
public
 
void  Fly() {}
}

 

or

 

class  NewGirl :  public  ChildT < Mother, NewAblity, GIRL > ,  public  Father
{
public
 
void  Fly() {}
}

 

To be continued?

posted on 2006-08-16 17:22 力为 阅读(1381) 评论(2)  编辑 收藏 引用 所属分类: 5. GP&STL&BOOST

评论

# re: An Adapter Solution 2006-09-05 16:43 taochunhua
太棒了!
这个故事有下文吗?
  回复  更多评论
  

# re: An Adapter Solution 2006-10-18 12:22 力为
@taochunhua
To be continued...  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理