C++ define elements of enum somewhere else in the code [closed]
I have a class in C++ with a public member function that I want to accept as argument a limited number of values and, since I want to make it easier for the user of my class to know what to pass to the member, I'm using an enum (but I'm not sure if there is a better solution) to give him a limited and clear set of things to pass to the function. I use this enum because it uses "words" instead of numbers to describe certain things, which is easier for the user of the class.
Two clarifications: 1) the enum, in the .h file, does not assign values to its elements but in reality it should because each of these words corresponds to a number, and 2) at the same time, I want that the user isn't necessarily aware of the numerical values that each "word" in the enum corresponds to. Therefore, I want to assign a value to each enum element somewhere else, for example in the .cpp file.
The enum is public defined in the .h file like this
enum ITEM{
item_1,
item_2,
item_3
};
The member function also public (can be called from outside of the cpp file) is defined in the .cpp file as:
void MyClass::memberFunction(ITEM item) {
switch (item){
case item_1:
otherFunction(1);
break;
case item_2:
otherFunction(2);
...
}
}
Is it possible to define (or have a sort of an override) the value of each item_i in the cpp file? Something like
ITEM{
item_1 = 1,
item_2 = 2,
item_3 = 45
};
This would also avoid the switch because in void MyClass::memberFunction(ITEM item) I can simply call otherFunction(item), if obviously all the item_i are of the same type of the argument accepted by otherFunction(item)
from Recent Questions - Stack Overflow https://ift.tt/HVEYMdc
https://ift.tt/4ajfIVs
Comments
Post a Comment