How to write a Java-enum-like class with multiple data fields in C++?

前端 未结 4 692
感情败类
感情败类 2020-11-30 21:32

Coming from a Java background, I find C++\'s enums very lame. I wanted to know how to write Java-like enums (the ones in which the enum values are objects, and can have attr

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 22:00

    May be this is what you want --

    #include
    
    using namespace std;
    
    class Planet {
        double mass,radius;
    
        Planet(double m, double r) : mass(m) : radius(r) {}
    
    public:
        static const Planet MERCURY;
    
        void show(){
            cout<

    This is just a small code, Im sure you can modify this to suit your needs..

提交回复
热议问题