我需要創建一個存儲多個用戶定義類型的類。它應該根據需要返回其中的一個。有沒有辦法實現一個函數來返回所有類型?根據請求存儲多種類型並返回單一類型
請注意:我無法使用Boost庫。我需要在Visual Studio中實現
class One {};
class Two {};
class Three {};
enum Type
{
OneType,
TwoType,
ThreeType
};
class GenericType
{
template <typename T> // --- How to implement this function
T getValue(Type type)
{
switch(type)
{
case One: return oneType; // Error
case Two: return twoType;
case Three: return threeType;
}
}
shared_ptr<OneType> oneType;
shared_ptr<TwoType> twoType;
shared_ptr<ThreeType> threeType;
Type m_type;
};
std :: get()只能獲取索引的項目(作爲參數提供) – Neo