Last active
July 25, 2019 18:12
-
-
Save Rcomian/15aa07df1478541f77d40bd09644d061 to your computer and use it in GitHub Desktop.
C++ decentralised enum types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENUM_CLASS::ENUM_CLASS() : id(nextid++) {} | |
int ENUM_CLASS::nextid = 0; | |
bool ENUM_CLASS::operator==(const ENUM_CLASS& other) const { | |
return id == other.id; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class ENUM_CLASS | |
{ | |
public: | |
ENUM_CLASS(); | |
bool operator==(const ENUM_CLASS& other) const; | |
protected: | |
const int id; | |
static int nextid; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Usage.h" | |
int main() { | |
ENUM_CLASS a = MyObject::ENUM_VALUE_A; | |
if (a == MyObject::ENUM_VALUE_A) { | |
std::cout << "success" << std::emdl; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "EnumBase.hpp" | |
struct MyObject { | |
static ENUM_CLASS ENUM_VALUE_A; | |
static ENUM_CLASS ENUM_VALUE_B; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment