如何在C++鏈表中鏈入不同類型對象
似乎你也注意到了,不管怎么定義,好像一個C++鏈表中的對象都是同一類型的。而實際上,這也是必須的,否則,返回節(jié)點(diǎn)中的數(shù)據(jù)這樣的函數(shù)的返回值的類型是什么呢?但是,人的要求是無止境的。把不同的對象鏈在一個鏈表中的目的是為了方便使用,現(xiàn)在一定記住這個原則,后面的討論都是基于這個原則的。
達(dá)到這個目標(biāo)的原理其實很簡單,只要把不同類型的對象變成同樣的類型就可以了??聪旅娴慕Y(jié)構(gòu)定義:
- struct Mobject
- {
- void *p;
- int ObjectType;
- };
將一個對象鏈入鏈表時,將指向這個對象的指針賦給p,同時記錄對象類型。當(dāng)取得這個節(jié)點(diǎn)的時候,根據(jù)ObjectType的值來確定p指的對象類型,從而還原指針類型,也就得到了原來的對象。
后面講到的廣義表實際上采用的就是這種方法。顯而易見的,這樣的Mobject支持的對象是預(yù)先確定的,你將自己維護(hù)ObjectType列表,每添加一種類型的支持,你需要在ObjectType列表中給出它的替代值,然后在相應(yīng)的switch(ObjectType)給出這種類型的case語句。很煩人是吧,下面給出另一種方法,其實還是這個原理,不同的是,把這個煩人的工作交給編譯器了。
還記得前邊強(qiáng)調(diào)的原則嗎,為什么我們將不同類型的對象放在一個鏈表中呢?很顯然,我們想達(dá)到這樣的一個效果:比如說,我們在一個鏈表中儲存了三角形,直線,圓等圖形的參數(shù),我們希望對某個節(jié)點(diǎn)使用Draw()方法,就重繪這個圖形;使用Get()則得到這個圖形的各個參數(shù);使用Put()則修改圖形的參數(shù)??梢钥闯觯@些不同的對象實際上有同樣的行為,只是實現(xiàn)的方法不同。
C++的多態(tài)性正好可以實現(xiàn)我們的構(gòu)想。關(guān)于這方面,請參閱相關(guān)的C++書籍(我看的是《C++編程思想》)。請看如下的例子:
- #ifndef Shape_H
- #define Shape_H
- class Shape
- {
- public:
- virtual void Input() = 0;
- virtual void Print() = 0;
- Shape(){};
- virtual ~Shape(){};
- };
- #endif
【說明】定義一個抽象基類,有兩個行為,Input()為輸入圖形參數(shù),Print()為打印圖形參數(shù)。圖省事,只是簡單的說明問題而已。
- #ifndef Point_H
- #define Point_H
- class Point
- {
- public:
- void Put()
- {
- cout << "x坐標(biāo)為:";
- cin >> x;
- cout << "y坐標(biāo)為:";
- cin >> y;
- }
- void Get()
- {
- cout << endl << "x坐標(biāo)為:" << x;
- cout << endl << "y坐標(biāo)為:" << y;
- }
- virtual ~Point(){};
- private:
- int x;
- int y;
- };
- #endif
【說明】點(diǎn)的類定義與實現(xiàn)。
- #ifndef Circle_H
- #define Circle_H
- #include "Shape.h"
- #include "Point.h"
- class Circle : public Shape
- {
- public:
- void Input()
- {
- cout << endl << "輸入圓的參數(shù)";
- cout << endl << "輸入圓心點(diǎn)的坐標(biāo):" << endl;
- center.Put();
- cout << endl << "輸入半徑:";
- cin >> radius;
- }
- void Print()
- {
- cout << endl << "圓的參數(shù)為";
- cout << endl << "圓心點(diǎn)的坐標(biāo):" << endl;
- center.Get();
- cout << endl << "半徑:" << radius;
- }
- virtual ~Circle(){};
- private:
- int radius;
- Point center;
- };
- #endif
【說明】圓的類定義與實現(xiàn)。繼承Shape類的行為。
- #ifndef Line_H
- #define Line_H
- #include "Shape.h"
- #include "Point.h"
- class Line : public Shape
- {
- public:
- void Input()
- {
- cout << endl << "輸入直線的參數(shù)";
- cout << endl << "輸入端點(diǎn)1的坐標(biāo):" << endl;
- point1.Put();
- cout << endl << "輸入端點(diǎn)2的坐標(biāo):" << endl;
- point2.Put();
- }
- void Print()
- {
- cout << endl << "直線的參數(shù)為";
- cout << endl << "端點(diǎn)1的坐標(biāo):";
- point1.Get();
- cout << endl << "端點(diǎn)2的坐標(biāo):";
- point2.Get();
- }
- virtual ~Line(){};
- private:
- Point point1;
- Point point2;
- };
- #endif
【說明】直線類的定義與實現(xiàn)。繼承Shape的行為。
- #ifndef ListTest_H
- #define ListTest_H
- #include
- #include "List.h"
- #include "Circle.h"
- #include "Line.h"
- void ListTest_MObject()
- {
- List
a; - Shape *p1 = new Circle;
- Shape *p2 = new Line;
- p1->Input();
- p2->Input();
- a.Insert(p1);
- a.Insert(p2);
- Shape *p = *a.Next();
- p->Print();
- delete p;
- a.Put(NULL);
- p = *a.Next();
- p->Print();
- delete p;
- a.Put(NULL);
- }
- #endif
【說明】這是測試函數(shù),使用方法是在含有main()的cpp文件頭部加入#include “ListTest.h”,然后調(diào)用ListTest_Mobject()。這是一個簡單的例子,可以看出,刪除這樣的鏈表節(jié)點(diǎn)需要兩個步驟,先delete鏈表節(jié)點(diǎn)data域里指針?biāo)傅膶ο?,然后才能刪除鏈表節(jié)點(diǎn)。同樣,析構(gòu)這樣鏈表的時候,也需要注意這個問題。不然的話,你的程序運(yùn)行一次內(nèi)存就少一點(diǎn)(可能不是這樣,據(jù)說操作系統(tǒng)在程序中止時可以回收動態(tài)內(nèi)存,但后面的結(jié)論是對的),如果是個頻繁調(diào)用的函數(shù),當(dāng)運(yùn)行一段時間后,你的系統(tǒng)就癱瘓了。所以,使用這樣的鏈表***是派生一個新的鏈表類,實現(xiàn)相應(yīng)的操作。例如這樣:
- class ShapeList : public List
- {
- public:
- BOOL SL_Remove()
- {
- Shape *p = *Get();
- delete p;
- return Remove();
- }
- };
【閑話】不知你是不是對這樣的語句Shape *p = *a.Next(); p->Print();不甚理解,還覺得有點(diǎn)羅嗦。那你試試這樣的語句*a.Next()->Print();能不能編譯通過。
【編輯推薦】