我正在研究一個C#項目,並發現自己試圖在C++中重現一個常見模式,但到目前爲止一直未能。什麼,我試圖做(在C++)的一個例子:等效的C++的typedefs /別名在模板編程中使用
enum ProductItemTypes {
Product,
Family,
SubFamily,
Department
};
class ProductItem : PanelItem {
public:
using item_type_t = ProductItemTypes;
...
};
enum PaymentItemTypes {
Group,
Valued,
NotValued,
Balance
};
class PaymentItem : PanelItem {
public:
using item_type_t = PaymentItemTypes;
...
};
有跡象表明,從PanelItem
繼承和擁有自己的item_subtype_t
不同類別。 然後,使用PanelItem
的類,可以知道相應的項目類型並一般工作。在C#中有某種類似的能力可以幫助我避免大量的重複代碼,但我不想通過所有類型(ProductItem
,ProductItemType
,以及我爲簡單排除的一些內容)每時每刻。
@Barry,我添加了[標籤:C++],因爲您必須能夠閱讀該源代碼。 – Sinatr
爲什麼不能:template class Panel { public: .... }; Then Panel foo; 最終你可以定義一個如上所述的類型。 –
nosbor
@nosbor是的,面板部分並不重要,重要的部分是'ProductItem'中的'item_subtype_t',因爲不同的'PanelItem'在那裏有不同的類型定義 –