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

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 愛上龍卷風 閱讀(2231) 評論(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在线精品视频在线观看| 欧美国产日韩一区二区| 久久视频国产精品免费视频在线| 亚洲欧美国产毛片在线| 亚洲欧美国产一区二区三区| 欧美亚洲尤物久久| 久久大综合网| 久久亚洲一区二区三区四区| 欧美91视频| 亚洲精品网站在线播放gif| 亚洲精品日韩激情在线电影| 中文欧美在线视频| 欧美一区二区三区的| 久久影音先锋| 欧美日韩一区二区视频在线 | 亚洲一区二区三区在线观看视频| 亚洲一二三区在线观看| 久久aⅴ国产欧美74aaa| 最新国产乱人伦偷精品免费网站| 亚洲免费视频一区二区| 亚洲一区二区精品在线| 久久国产欧美日韩精品| 欧美国产先锋| 亚洲欧美国产77777| 久久亚洲精品欧美| 欧美日韩美女一区二区| 国产综合色在线| 99国产精品一区| 久久嫩草精品久久久久| 99视频在线精品国自产拍免费观看| 欧美一级理论性理论a| 欧美激情性爽国产精品17p| 国产亚洲欧洲一区高清在线观看 | 一区二区三区**美女毛片| 欧美一区二区| 欧美亚洲成人精品| 亚洲精选成人| 久久免费精品视频| 亚洲视频在线观看视频| 裸体素人女欧美日韩| 国产欧美 在线欧美| 亚洲精品乱码久久久久| 麻豆精品一区二区综合av| 亚洲午夜精品一区二区三区他趣| 久久亚洲美女| 国产午夜精品麻豆| 亚洲中字在线| 99精品欧美一区| 欧美精品一区二区三区高清aⅴ| 黄色成人91| 欧美中文字幕视频在线观看| 在线视频你懂得一区二区三区| 美女诱惑一区| 亚洲国产精品视频| 免费在线亚洲欧美| 久久国产主播| 一区二区三区在线免费视频 | 亚洲国产婷婷香蕉久久久久久| 久久久久久九九九九| 国产亚洲一区二区三区| 久久精品国产999大香线蕉| 中文无字幕一区二区三区| 欧美日韩视频在线第一区| 一本久久青青| 99精品热6080yy久久| 欧美日韩国产美| 国产精品99久久99久久久二8| 亚洲成人资源网| 欧美成人午夜| 一本色道久久综合| 亚洲精品中文字幕有码专区| 欧美日韩黄视频| 亚洲一区国产一区| 亚洲一区二区三区涩| 免费精品99久久国产综合精品| 欧美成人免费观看| 亚洲精品自在久久| 欧美日韩在线视频一区二区| 性做久久久久久久免费看| 欧美大片专区| 久久久中精品2020中文| 国产精品s色| 久久综合网络一区二区| 亚洲高清在线| 韩国精品一区二区三区| 亚洲免费在线电影| 亚洲欧美日韩一区二区在线| 国产嫩草影院久久久久| 久久人人看视频| 欧美大片免费观看在线观看网站推荐| 亚洲精品一区在线观看| 一区二区三区四区五区视频 | 欧美三区免费完整视频在线观看| 亚洲一级网站| 欧美一区亚洲一区| 亚洲精品综合久久中文字幕| 午夜伦理片一区| 亚洲乱码国产乱码精品精| 午夜精彩国产免费不卡不顿大片| 激情国产一区| 一区二区三区欧美日韩| 亚洲国产成人久久综合| 亚洲一区二区三区视频| 亚洲精品中文字幕有码专区| 亚洲视频一区二区在线观看 | 久久久国产成人精品| 一本久久综合亚洲鲁鲁| 久久免费国产| 久久福利一区| 国产精品成av人在线视午夜片| 开心色5月久久精品| 欧美视频网站| 91久久在线视频| 精品电影在线观看| 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲国产精品va| 久久丁香综合五月国产三级网站| 亚洲专区欧美专区| 欧美日韩国产在线观看| 亚洲国产高清一区二区三区| 一区二区在线免费观看| 午夜免费电影一区在线观看| 亚洲影院免费| 欧美日韩免费在线| 亚洲二区在线| 亚洲国产精品一区二区尤物区| 欧美一区二区三区免费观看| 亚洲视频福利| 一本色道久久综合亚洲精品婷婷 | 欧美激情1区2区3区| 国产在线精品一区二区中文| 亚洲欧美成人| 香蕉久久久久久久av网站| 欧美色123| 亚洲免费精品| 一区二区三区精品在线| 欧美精品色综合| 亚洲精品日本| 亚洲四色影视在线观看| 欧美日韩精品一二三区| 亚洲精品日韩欧美| 亚洲在线观看免费| 国产精品综合色区在线观看| 亚洲欧美日韩一区在线观看| 久久综合九色| 亚洲电影中文字幕| 欧美黄色一区二区| 日韩一区二区精品葵司在线| 亚洲在线免费视频| 国产视频一区欧美| 久久久欧美一区二区| 欧美不卡一卡二卡免费版| 亚洲人成在线观看网站高清| 欧美国产日韩视频| 一区二区三区 在线观看视| 欧美在线一级va免费观看| 韩国av一区二区三区| 欧美大秀在线观看| 一区二区三区日韩精品视频| 欧美在线综合| 国产在线视频欧美一区二区三区| 久久精品30| 亚洲日本欧美在线| 午夜老司机精品| 在线观看国产成人av片| 欧美日韩国产美| 欧美一区二区三区婷婷月色| 欧美电影专区| 亚洲综合首页| **性色生活片久久毛片| 欧美日韩另类字幕中文| 欧美在线视频观看| 亚洲国产精品精华液2区45| 亚洲欧美999| 亚洲东热激情| 国产精品丝袜白浆摸在线| 久久精品视频一| 一本久久青青| 欧美激情一区二区三区在线视频观看 | 欧美另类69精品久久久久9999| 亚洲一区二区三区欧美| 欧美激情久久久| 欧美呦呦网站| 夜夜嗨av一区二区三区免费区 | 亚洲第一视频| 国产精品一区二区在线| 欧美成年人视频| 先锋影音久久久| 99这里只有久久精品视频| 美国十次了思思久久精品导航| 国产精品99久久久久久久久| 欧美大片91| 亚洲中午字幕| 国产精品视频yy9299一区| 免费观看成人| 欧美影院视频| 亚洲欧美国产不卡| 一本一本a久久|