-
enumeration question
How can I check for an enum type in another program. For example I have the following enumeration:
Code:
class Processing_Plants
{
public:
enum Juice_t
{
Orange,
Grapefruit
};
}; // end class Processing_Plants
I would like to be able to check to see if "Orange" or "Grapefruit" are the current state by use of an if statement and then perform some operation. My problem is this. I don't know the proper syntax for performing this check. Thanks in advance!
JK
-
For ease lets move it out of the class and into the global scope:
Code:
enum Juice_t { Orange, Grapefruit };
int main()
{
Juice_t ju = Grapefruit;
if ( ju == Orange )
cout << "Meh?"<<endl;
else
cout << "Ok this is what we expected"<<endl;
}
If you leave it in the class you'll have to resolve the scope with:
Code:
Processing_Plants::Juice_t
and
Code:
Processing_Plants::Orange
-
Code:
void typeTest(Processing_Plants::Juice_t type)
{
if(type == Processing_Plants::Orange)
cout << "It's Orange" << std::endl;
else if(type == Processing_Plants::Grapefruit)
cout << "It's Grapefruit - Yummy!" << std::endl;
}
**EDIT**
Beaten.
-
Very nice explanation. Thank you :D :D !
-
ooops! How would I declare and call this function?
-
I was able to figure out how to declare it but I can't figure our the correct syntax use for calling it.
-
here is the funtion:
Code:
void Citrus::typeTest(Processing_Plants::Juice_t type)
{
if(type == Processing_Plants::Orange)
cout << "It's Orange" << std::endl;
else if(type == Processing_Plants::Grapefruit)
cout << "It's Grapefruit - Yummy!" << std::endl;
}
What is the proper syntax for calling this function? Thanks in Advance!
-
Well lets look at a near example:
Code:
void typeTest(int i)
{
if ( i==1 )
cout<<"Its a 1"<<endl;
else if (i==2)
cout<<"Its a 2"<<endl;
}
void somefunc()
{
int x = 1;
typeTest(x);
}
I bolded for a reason.
-
I understand the passing of an "int" type into the function into the function you have provided. What is not clear to me is passing an "enum" type as shown in the function I have provided. Please show a sample of how this is accomplished. Thanks in advance!
JK ;)
-
It's exactly the same. Replace 'int' with 'Juice_t', '1' with 'Orange' and '2' with 'Grapefruit'.
-
Just change the types from int to the enum type (which you've already done in your function). Change the constant int values to the enum values (again which you've already done in your function). Enums are just values and can be passed around as such.
-
My call is hosed! Please see my code below that sjows the call to the function and the function itself.
Code:
void somefunc()
{
typeTest(Juice_t);
}
void Citrus::typeTest(Processing_Plants::Juice_t type)
{
if(type == Processing_Plants::Orange)
cout << "It's Orange" << std::endl;
else if(type == Processing_Plants::Grapefruit)
cout << "It's Grapefruit - Yummy!" << std::endl;
}
-
Code:
void somefunc()
{
typeTest(Juice_t);
}
You must pass an actual value, not the type.
Code:
void somefunc()
{
typeTest(Processing_Plants::Orange);
}
-
I appreciate your patience! I really do. If there are two possibilities (ie - Orange or Grapefruit), isn't what you suggested 'hard coding' the value being passed?
-
Yeah, but this call is just an example anyway. How you come up with the initial value to pass (Orange or Grapefruit or Apple) is up to you.