2016-08-04 98 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