I sometimes run into situations where i have an enum that has a string constant associated with it and at points in the code I have to replace the enum with the string. e.g.
An enum in my code
typedef enum {
//define weapon names
kWeaponGaussRifleType = 1,
kWeaponGatlingGunType,
kWeaponSideWinderMissileType,
kWeaponLaserType
} WeaponType;
Common use for that enum which is perhaps ok.
void fireWeapon(WeaponType w) {
switch(w) {
...
}
}
Possible incorrect use of that enum which I would like to fix.
void loadAsset(WeaponType w) {
//associate weapon filename with the weapon type
if(w == kWeaponGaussRifleType) {
fileName = "gaussRifle.png"
} elseif {
...
}
}
Obviously this code above can be replaced with a switch or it could pick names from an array of strings. But it would still mean that I have to define the entity name twice (once in the enum and then in the code where the translation occurs) and this is thus prone to error and inconsistency in naming.
Is there any way in C++ to fix this?