I'm making a enum in c++ to make a finite state machine using binary flags. It looks like:
enum VStates
{
NEUTRAL = 0x00000000, // 000000
// Physical Status
DRY = 0x00000001, // 000001
WET = 0x00000002, // 000010
HOT = 0x00000004, // 000100
COLD = 0x00000008, // 001000
BURNED = 0x00000016, // etc..
FROZEN = 0x00000032,
EROS = 0x00000064, //
THANATOS = 0x00000128, //
SLEEP = 0x00000256,
STUNNED = 0x00000512,
PARALYZED = 0x00001024,
POISONED = 0x00002048, //
BLIND = 0x00004096,
SOFT = 0x00008192, // Flexible
TOUGH = 0x00016384, // Resistent
MAGNETIZED = 0x00032768,
POSSEDERUNT = 0x00131072, //
// Mental Status
ANGRY = 0x00262144,
DRUGGED = 0x00524288, // Drugs Meaning
HORNY = 0x01048576, // Sexual Meaning
// Material Status
METAL = 0x02097152,
WOOD = 0x04194304,
GLASS = 0x08388608,
AIR = 0x16777216,
EARTH = 0x33554432,
DUST = 0x67108864,
LIGHT = 0x134217728,
SHADOW = 0x268435456,
WATER = 0x536870912,
// Total Status
PROTECTED = 0x1073741824,
INVULNERABLE = 0x2147483648
};
Some status are incompatibles, so I use Bitwise operators to manage them. Now, my compiler say:
warning: integer constant is too large for 'long' type
Is this the correct way to declare this enum? I like avoid warning so, How can I resolve this problem?
In theory, an ENUM column can have a maximum of 65,535 distinct values; in practice, the real maximum depends on many factors.
The size of an enum is compiler-specific and should default to the smallest integral type which is large enough to fit all of the values, but not larger than int. In this case, it seems the sizeof enum is fixed at 16-bit. For starters, that wastes a heck of a lot of memory if you use a boolean typedef (which I do).
Custom values to the constants To hold the value of each constant you need to have an instance variable (generally, private). You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once.
If values are not assigned to enum members, then the compiler will assign integer values to each member starting with zero by default. The first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member.
In C++11, you can specify the underlying type of the enum.
#include <cstdint>
enum VStates : uint64_t {
// Values
}
On a side note, I recommend against calculating out all of those powers of two. You made an error in your calculations by writing a hex constant but giving it the digits of a base-10 numeral. However, rather than recalculating all of those, I recommend something like:
#include <cstdint>
enum VStates : uint64_t {
NEUTRAL = 0ULL,
DRY = 1ULL << 0,
WET = 1ULL << 1,
HOT = 1ULL << 2,
COLD = 1ULL << 3,
// etc.
}
Then you are sure not to make a mistake. The ULL
suffix ensures that the literal is accepted as at least a 64-bit wide integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With