青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

sherrylso

C++博客 首頁 新隨筆 聯系 聚合 管理
  18 Posts :: 0 Stories :: 124 Comments :: 0 Trackbacks

1. 什么是Multi-methods.

在闡述這個概念之前,我們先看一下什么是多態(Polymorphisn)。多態是面向對 象程序設計的一個重要的特征, 多態是允許你將父對象的指針(或者引用)設置成為它的子對象的技術,賦值之后,該父對象指針(或者引用)就可以根據當前賦 值給它的子對象的特性以不同的方式運作。簡單的說,就是一句話:允許將子類類型的指針賦值給父類類型的指針。多態性在C++中都是通過虛函數 (Virtual Function) 實現的。 例如:

class Parent
...
{
    
public:
    
virtual void Test() = 0;
}


class Sub1: public Parent
...
{
public:
   
void Test() 
   ...
{
        cout
<<"HI, this is sub1"<<endl;
   }

}

class Sub2: public Parent
...
{
   
void Test() 
   ...
{
        cout
<<"HI, this is sub2"<<endl;
   }

}


Parent
*  p1 = new Sub1();
Parent
*  p2 = new Sub2();
p1
->Test(); //call the Test method of class Sub1;
p2->Test();//call the Test method of class Sub1;

在c ++中,多態性的內涵是:通過virtual function技術,真實的函數調用完全依賴于對象的真實類型(這就是late binding)。我們的問題是:virtual function調用可不可以依賴于兩個或者兩個以上的對象?這就是multi-methods.

例子:

//method declaration
bool intersect(const Shape&const Shape&,)
...
{
//...
}

bool intersect(const Rectangle&const Circle&,)
...
{
//...
}


class Shape
...
{
//...
}

class Rectangle: public Shape
...
{
//...
}

class Circle: public Shape
...
{
//...
}


Shape
* s1 = new Rectangle();
Shape
* s2 = new Circle();

//while calling the following method, what happend?
//the actual function which will be called is bool intersect(const Shape&, const Shape&,), not bool intersect(const Rectangle&, const Circle&,)
// but what we want is the latter, not former
intersect(*s1,*s2);
//maybe you can call like this, which will meet our requirement, but, unfor?tunately , it violats the principle: we should program based on interface not implementation.
intersect(*(dynamic_cast<Rectangle*>(s1)),*(dynamic_cast<Circle*>(s2)));



2. C++ committee曾經考慮的解決方案(摘自 The Design and Evolution of C++, by Stroustrup)

c++從語言自身來支持multi-method:

//solution 1:
(s1@s2)->intersect();  //rather than intersect(s1,s2);
//solution 2:
bool intersect(virtual const Shape&,virtual const Shape&);
bool intersect(virtual const Rectangle&,virtual const Circle&//overrides
{
}

或許,C++在將來的某個版本會支持multi-method.

3. workarounds for multi-method.

3.1 用double dispatch設計模式解決。

double dispatch(雙分派)設計模式是指:在選擇一個方法的時候,不僅僅要根據消息接收者(receiver)的運行時型別(Run time type,還要根據參數的運行時型別(Run time type。接下來我們用double dispatch來解決一下上面的那個問題:

class Shape
...
{
//...
virtual bool intersect(const Shape&const = 0;
virtual bool intersect(const Rectangle&const = 0;
virtual bool intersect(const Circle&const =0;
}

class Rectangle: public Shape
...
{
//...
bool intersect(const Shape& s) const
...
{
   
return s.intersect(*this); // *this is a Rectangle and calling which intersect method totally depends on the real type of s!
}

bool intersect(const Rectangle&const 
...
{
//...
}

bool intersect(const Circle&const
...
{
//...
}

}

class Circle: public Shape
...
{
//...
bool intersect(const Shape& s) const
...
{
   
return s.intersect(*this); // *this is a Circle and calling which intersect method totally depends on the real type of s!
}

bool intersect(const Rectangle&const 
...
{
//...
}

bool intersect(const Circle&const
...
{
//...
}

}

對于double dispatch, 最大的問題是:這樣的設計與類的繼承結構高度耦合,當類的繼承結構改變后,會影響到現有的代碼。從上面的例子可以看到:Shape 類是不應該意識到它的子類的存在。不過當前的主流面向對象程序設計語言(C++/Java/C#等)都并不支持multi-method, 從這個意義上說:double dispatch還是一個合理,有效地解決方案

3.2 另一個可選的方案是RTTI。

bool intersect(const Shape& s1, const Shape& s2)
{
//the follwing code, can implement by using index table, the key is the type_id of the real object(such as typeid(s1) and typeid(s2))
//the query result is the pointer to function (such as the pointer to function: bool intersect(const Rectangle&, const Circle&).
if( typeid(s1)==typeid(Rectangle) && typeid(s1)==typeid(Rectangle))
{
     intersect(
*(dynamic_cast<Rectangle*>(s1)),*(dynamic_cast<Circle*>(s2)));
}

else if(...)
{
//...
}


}

 
bool intersect(const Rectangle&const Circle&,)
{
//...
}


class Shape
......
{
//...
}

class Rectangle: public Shape
......
{
//...
}

class Circle: public Shape
......
{
//...
}


Shape
* s1 = new Rectangle();
Shape
* s2 = new Circle();
//as a result , for the caller, the code is based on interface not implementation
intersect(*s1,*s2);





posted on 2007-05-06 22:28 愛上龍卷風 閱讀(2232) 評論(4)  編輯 收藏 引用

Feedback

# re: C++與double dispatch設計模式 2009-06-26 18:17 Dan
很好!明白這個東西了。  回復  更多評論
  

# re: C++與double dispatch設計模式[未登錄] 2011-01-08 14:53 darren
個人感覺還是RTTI比較好,代碼比較簡潔一點。  回復  更多評論
  

# re: C++與double dispatch設計模式 2012-02-03 14:52 croma
這樣的意義何在呢?
抽象行為的精神不就是簡化程式碼嗎?

正常的情況下 應該是 Shape 的 class 定義出形狀的描述規則
並且在 Intersect 實做這個規則的計算方法

子類別只要實作出自己的形狀描述就好了

我們不在意執行階段他是什麼型態,只要他完成 Shape 的描述就好了













  回復  更多評論
  

# re: C++與double dispatch設計模式 2013-09-05 01:52 Wangsu

@croma

Shape shapes[]={new Circle(),new Rect(), new Something()...};

foreach(Shape shape in shapes)
{
intersect(shape,next shape);<<=這裡會在 Compiler time 就決定調用 bool intersect(const Shape&, const Shape&,)。
}
  回復  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久尤物电影视频在线观看| 国产精品99久久久久久有的能看| 久久精品视频在线免费观看| 激情欧美一区二区| 蜜桃久久精品一区二区| 欧美不卡视频| 亚洲综合社区| 久久gogo国模啪啪人体图| 亚洲国产精品激情在线观看| 亚洲国产欧美久久| 欧美精品亚洲二区| 欧美亚洲视频在线看网址| 午夜视频一区二区| 亚洲国产精品成人综合| 99re6热在线精品视频播放速度| 欧美精品粉嫩高潮一区二区 | 日韩一级免费| 亚洲欧美中文字幕| 91久久精品久久国产性色也91| 日韩午夜在线视频| 狠狠久久婷婷| 一本色道久久88综合亚洲精品ⅰ| 国产婷婷97碰碰久久人人蜜臀| 欧美 日韩 国产精品免费观看| 欧美日韩亚洲一区二区三区在线 | 亚洲区一区二| 国产亚洲成av人片在线观看桃| 欧美va天堂va视频va在线| 欧美日韩亚洲系列| 欧美ab在线视频| 国产欧美精品在线| 日韩一级免费观看| 亚洲高清视频一区| 欧美一区二区在线看| 亚洲一区在线免费观看| 狂野欧美激情性xxxx| 久久国产精品99精品国产| 欧美风情在线| 欧美99久久| 国产最新精品精品你懂的| 亚洲视频成人| 亚洲桃色在线一区| 欧美久久久久中文字幕| 欧美激情麻豆| 亚洲国产精品热久久| 欧美有码在线观看视频| 亚洲欧美一区二区三区在线| 欧美精品啪啪| 亚洲激情一区| 亚洲毛片网站| 欧美国产日韩xxxxx| 免费亚洲网站| 在线成人性视频| 久久久九九九九| 久久久久久尹人网香蕉| 国产欧美激情| 欧美一级精品大片| 欧美自拍偷拍午夜视频| 国产精品视频第一区| 亚洲免费一在线| 欧美一区二区在线| 国产欧美在线| 久久国产精品网站| 老色鬼久久亚洲一区二区| 国产一区二区中文字幕免费看| 午夜精品久久久久久久久久久久久 | 欧美另类久久久品| 欧美激情中文字幕在线| 亚洲激情成人在线| 欧美日韩成人综合在线一区二区 | 亚洲视频在线播放| 亚洲欧美国产精品专区久久| 国产精品免费福利| 亚洲伦理在线免费看| 国产精品v欧美精品∨日韩| 久久一区二区三区国产精品| 国色天香一区二区| 久久深夜福利免费观看| 亚洲国产va精品久久久不卡综合| 日韩亚洲欧美综合| 国产精品r级在线| 久久激情视频久久| 亚洲韩日在线| 亚洲免费网址| 在线观看视频一区二区| 欧美激情一区在线| 亚洲影院色无极综合| 免费美女久久99| 在线综合亚洲欧美在线视频| 国产日韩欧美一区二区三区在线观看 | 一区二区三区精品视频| 久久精品人人做人人爽电影蜜月| 在线精品国精品国产尤物884a| 欧美**人妖| 亚洲在线免费| 亚洲国产欧美日韩精品| 午夜精品久久久久久99热| 亚洲第一久久影院| 国产精品久久久999| 久久午夜国产精品| 亚洲一区二区少妇| 欧美大片免费看| 午夜一区二区三区在线观看| 在线观看精品一区| 国产欧美日韩亚州综合| 欧美日韩国产系列| 久久久综合精品| 亚洲一级黄色| 亚洲麻豆视频| 亚洲福利视频二区| 久久综合狠狠综合久久综青草| 中日韩男男gay无套| 亚洲电影av在线| 国产亚洲aⅴaaaaaa毛片| 欧美日韩一区二区精品| 久久综合999| 久久精品99无色码中文字幕 | 久久综合激情| 欧美在线一级视频| 亚洲香蕉伊综合在人在线视看| 极品尤物久久久av免费看| 国产精品自拍在线| 国产精品va| 欧美午夜不卡视频| 欧美日韩在线一区二区| 欧美黄色小视频| 毛片一区二区| 美女网站久久| 久久综合精品国产一区二区三区| 欧美一区二区三区免费视| 国产精品99久久久久久人| 日韩一级在线| 99精品欧美一区| 夜夜嗨av一区二区三区四季av| 亚洲激情视频网| 亚洲激情午夜| 亚洲精选在线| 99re热这里只有精品免费视频| 亚洲国产综合在线| 亚洲国产精品123| 亚洲国产欧美国产综合一区| 亚洲第一视频网站| 亚洲人成高清| 亚洲欧美成人精品| 午夜精品久久久久久久久久久| 亚洲欧美日韩爽爽影院| 午夜精品久久久久| 久久久国产精品一区二区三区| 久久久久久一区| 欧美第一黄网免费网站| 亚洲激情av| 亚洲午夜视频| 久久精选视频| 欧美另类亚洲| 国产欧美在线看| 亚洲国产精品成人va在线观看| 亚洲精品免费一区二区三区| 日韩一本二本av| 午夜精品美女自拍福到在线| 久久精品一区| 欧美大片免费| 亚洲一级网站| 久久尤物视频| 国产精品igao视频网网址不卡日韩| 国产噜噜噜噜噜久久久久久久久| 狠狠色丁香久久婷婷综合丁香| 亚洲黄色视屏| 欧美一级久久久久久久大片| 麻豆精品视频| 在线一区二区三区四区| 久久超碰97中文字幕| 欧美激情国产日韩精品一区18| 国产精品v欧美精品v日韩 | 国产偷自视频区视频一区二区| 在线成人激情| 午夜影院日韩| 欧美激情亚洲一区| 亚洲综合色自拍一区| 蜜臀va亚洲va欧美va天堂| 国产精品欧美日韩一区| 亚洲国产精品传媒在线观看| 午夜精品久久久99热福利| 欧美激情亚洲另类| 欧美在线free| 欧美日韩在线一区二区| 在线欧美视频| 久久久久一区二区| 亚洲网站视频| 欧美激情第1页| 娇妻被交换粗又大又硬视频欧美| 亚洲无亚洲人成网站77777| 麻豆成人小视频| 亚洲综合色视频| 欧美系列精品| 日韩视频在线观看国产| 麻豆久久婷婷| 久久av一区二区三区| 国产精品乱码一区二三区小蝌蚪 | 久久久人成影片一区二区三区| 一区二区高清视频在线观看|