enum class constructor c++ , how to pass specific value?

后端 未结 2 1922
说谎
说谎 2020-12-06 23:28

I came from Java and here we have such option as set value to constuctor.

Example

enum TYPE
{
    AUTO(\"BMW\"),
    MOTOCYCLE(\"Kawasaki\");

    pr         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 23:48

    In C++ a class has to be created:

    class TYPE 
    {
    public:
        static const TYPE AUTO;
        static const TYPE MOTOCYCLE;
    
    private:
        std::string mBrandName;
    
        TYPE(std::string iBrandName)
            : mBrandName(iBrandName)
        {}
    
        TYPE(const TYPE&) = default;
        TYPE(TYPE&&)      = default;
        TYPE& operator=(const TYPE&) = default;
        TYPE& operator=(TYPE&&) = default;
        ~TYPE() = default;
    
    public:
        std::string getBrandName() { return mBrandName; }
    
        static TYPE getMotocycle() { return MOTOCYCLE; }
        static TYPE getAuto() { return AUTO; }
    };
    
    const TYPE TYPE::AUTO("BMW");
    const TYPE TYPE::MOTOCYCLE("Kawasaki");
    

    But this doesn't have the benefits of an enum (automatic numbering, ordering, conversions,...)

提交回复
热议问题