Skip to content

Instantly share code, notes, and snippets.

@guohai
Forked from anonymous/c++_enum.cc
Created January 7, 2013 15:30

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 7, 2013.
    30 changes: 30 additions & 0 deletions c++_enum.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #define DECLARE_ENUM(E) struct E { public: E(int value = 0) : _value((__Enum)value) { } E& operator=(int value) { this->_value = (__Enum)value; return *this; } operator int() const { return this->_value; } enum __Enum {
    #define END_ENUM() }; private: __Enum _value; };

    #include <iostream>

    using namespace std;

    class B {
    B &operator=(const B &);
    };

    class A {
    B member;
    };

    DECLARE_ENUM(FileSystem)
    Read = 0x1,
    Write = 0x2
    END_ENUM()

    DECLARE_ENUM(DataBase)
    Read = 0x1,
    Write = 0x2
    END_ENUM()

    int main() {
    FileSystem hello = FileSystem::Read;
    cout << hello << endl;
    return 0;
    }