我有下面的類:使用C++鏈表模板
typedef struct Listable
{
struct Listable *next;
struct Listable *prev;
// Lots of other class members not pertaining to the question excluded here
} Listable;
,我從它繼承像這樣:
typedef struct Object : Listable
{
} Object;
問題是,當我做這樣的事情:
Object *node;
for (node = objectHead; node; node = node->next);
我得到'node = node-> next'的錯誤,因爲node-> next的類型爲Listable,而節點的類型爲Object。
如何使用模板,在可列基類,使上一頁&下一個指針更改其類型的類被使用?
也許是這樣的:
typedef struct Listable<T>
{
struct Listable<T> *next;
struct Listable<T> *prev;
// Lots of other class members not pertaining to the question excluded here
} Listable;
,我從它繼承像這樣:
typedef struct Object : Listable<Object>
{
} Object;
我有超過10年的C,但是是相當新的C++模板一樣的功能。所以我不確定我應該使用什麼語法。
是的,我意識到,這些對象可以一次只屬於一個列表。這是設計。 – user1054922
只是讓你知道有一個內置的鏈接列表 – aaronman
不需要在C++中使用'typedef struct'。 'struct'就足夠了。 –