2016-08-04 101 views
0

我有以下C#代碼,我需要將其轉換爲C++代碼。我已經搜索了一些關於如何做C++枚舉的屬性,但無法弄清楚。C++枚舉屬性如C#

基本上我需要一種方法來表示C++中的以下簡化的C#代碼,它將使用屬性枚舉。

C#代碼:

public class PSMNameAttribute : Attribute 
     { 
      public string PSMName; 
      public PSMNameAttribute(string _PSMName) { PSMName = _PSMName; } 
     } 

public class PSMNumberAttribute : Attribute 
     { 
      public string PSMNumber; 
      public PSMNumberAttribute(string _PSMNumber) { PSMNumber = _PSMNumber; } 
     } 

public class PSMNumberNameAttribute : Attribute 
     { 
      public string PSMNumberName; 
      public PSMNumberNameAttribute(string _PSMNumberName) { PSMNumberName = _PSMNumberName; } 
     } 


public enum ShippingMethodsTypes 
     { 
      [PSMName("ErrorScriptMed")] 
      [PSMNumber("-5")] 
      [PSMNumberName("-5 ErrorScriptMed")] 
      ErrorScriptMed = -5     
      , 
      [PSMName("SpecialHandling")] 
      [PSMNumber("-1")] 
      [PSMNumberName("-1 SpecialHandling")] 
      SpecialHandling = -1     
      , 
      [PSMName("Error")] 
      [PSMNumber("0")] 
      [PSMNumberName("0 Error")] 
      Error = 0       
     } 

難道這是這樣做:

enum Category{ 
    unknown = -1, meat, poultry, seafood, dairy, vegetable,fruit, grain, sweet 
}; 

typedef struct { 
    float calories; // calories 
    float carbonhydrates; // grams 
    float fat; // grams 
    float cholesterol; // grams 
    float sodium; // grams 
    float protein; // grams 
    Category category ; 
}Food; 

如果是的話我怎麼會叫用枚舉的結構值?

+0

「枚舉與檢索此屬性屬性「並不代表C++中的任何東西,所以你必須解釋你想要做什麼。 –

+0

@PeteBecker謝謝你的repsonse。我需要繼續在C++中使用枚舉來表示Error = 0等基本知識,但對於枚舉錯誤,我需要在其上放置更多屬性,以便從中訪問它。基本上Error.PSNNumberName =「0錯誤」 – bing281

回答

1

boost::variant和一些遊客應該很好地解決這個問題:

#include <boost/variant.hpp> 
#include <iostream> 

struct get_number : boost::static_visitor<int> { 
    template<class Method> int operator()(const Method& m) const { return number(m); } 
}; 

struct get_name : boost::static_visitor<std::string> { 
    template<class Method> const std::string operator()(const Method& m) const { return name(m); } 
}; 

struct ShippingMethodMed {}; 
static constexpr int number(ShippingMethodMed) { return -5; } 
static std::string name(ShippingMethodMed) { return "ErrorScriptMedMed"; } 

struct ShippingMethodSpecialHandling { }; 
static constexpr int number(ShippingMethodSpecialHandling) { return -10; } 
static std::string name(ShippingMethodSpecialHandling) { return "SpecialHandling"; } 

struct ShippingMethodError {}; 
static constexpr int number(ShippingMethodError) { return 0; } 
static std::string name(ShippingMethodError) { return "Error"; } 

using ShippingMethod = boost::variant<ShippingMethodMed, ShippingMethodSpecialHandling, ShippingMethodError>; 

int number(ShippingMethod const& sm) { 
    return boost::apply_visitor(get_number(), sm); 
} 

std::string name(ShippingMethod const& sm) { 
    return boost::apply_visitor(get_name(), sm); 
} 

std::string number_name(ShippingMethod const& sm) { 
    return std::to_string(number(sm)) + " " + name(sm); 
} 


int main() 
{ 
    ShippingMethod m = ShippingMethodError(); 

    std::cout << number(m) << std::endl; 
    std::cout << name(m) << std::endl; 
    std::cout << number_name(m) << std::endl; 

    m = ShippingMethodSpecialHandling(); 

    std::cout << number(m) << std::endl; 
    std::cout << name(m) << std::endl; 
    std::cout << number_name(m) << std::endl; 
} 
+0

我喜歡你要去的地方,但不幸的是我不能離開枚舉,因爲這是在一個被許多其他程序調用的DLL。我需要添加屬性的枚舉,同時保持枚舉。 – bing281

1

這是不可能單獨枚舉。 不過我想你可以通過保持一個字符串數組/矢量/圖或者那些你枚舉等的組合解決這個問題:

enum test 
{ 
    first = 0, 
    second, // = 1 
    // etc... 
}; 

const char* attributes[] = 
{ 
    "first attribute", 
    "second attribute", 
}; 

,那麼你可以通過

const char* firstattribute = attributes[test::first]; 
+0

我相信像這樣可以工作。我感謝你的回答和幫助:) – bing281