Combine enum values
-
Hope somebody can help as I'm pulling my hair out here:mad: I normally code in C# but have had to use C++ and STL for project and having difficulty in finding out how to combine enum values in C++. In C# I'd | the values together e.g.
r = VALUE1; r = r|VALUE2;
doing this then allows me to check the enum later to see what values it matches, an example of this would possibly be a validtion routing that returns a single enum value but that value could contain multiple values indicating multiple failures. I've been googling and found code examples that show enum's being combined in the same way, but when I try to do it the compiler gives me the following error: error C2676: binary '|=' : 'Contract::ValidInput' does not define this operator or a conversion to a type acceptable to the predefined operator I've looked at operator overloading but to be honest overloading the | operator is beyond my C++ skills, which are limited at best. Can anybody help me out whilst I still have some hair? -
Hope somebody can help as I'm pulling my hair out here:mad: I normally code in C# but have had to use C++ and STL for project and having difficulty in finding out how to combine enum values in C++. In C# I'd | the values together e.g.
r = VALUE1; r = r|VALUE2;
doing this then allows me to check the enum later to see what values it matches, an example of this would possibly be a validtion routing that returns a single enum value but that value could contain multiple values indicating multiple failures. I've been googling and found code examples that show enum's being combined in the same way, but when I try to do it the compiler gives me the following error: error C2676: binary '|=' : 'Contract::ValidInput' does not define this operator or a conversion to a type acceptable to the predefined operator I've looked at operator overloading but to be honest overloading the | operator is beyond my C++ skills, which are limited at best. Can anybody help me out whilst I still have some hair?Something like this doesn't work?
enum TestEnum
{
Value1 = 0x00000001,
Value2 = 0x00000002,
Value3 = 0x00000004,
Value4 = 0x00000008,
Value5 = 0x00000010,
Value6 = 0x00000020,
Value7 = 0x00000040,
Value7 = 0x00000080
};int r = Value1;
r |= Value2;// or
int r = static_cast<int>(Value1);
r |= static_cast<int>(Value2);//*edit* or
TestEnum r = Value1;
r = static_cast<TestEnum>(r | Value2);-- modified at 11:18 Tuesday 8th May, 2007
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hope somebody can help as I'm pulling my hair out here:mad: I normally code in C# but have had to use C++ and STL for project and having difficulty in finding out how to combine enum values in C++. In C# I'd | the values together e.g.
r = VALUE1; r = r|VALUE2;
doing this then allows me to check the enum later to see what values it matches, an example of this would possibly be a validtion routing that returns a single enum value but that value could contain multiple values indicating multiple failures. I've been googling and found code examples that show enum's being combined in the same way, but when I try to do it the compiler gives me the following error: error C2676: binary '|=' : 'Contract::ValidInput' does not define this operator or a conversion to a type acceptable to the predefined operator I've looked at operator overloading but to be honest overloading the | operator is beyond my C++ skills, which are limited at best. Can anybody help me out whilst I still have some hair? -
Hope somebody can help as I'm pulling my hair out here:mad: I normally code in C# but have had to use C++ and STL for project and having difficulty in finding out how to combine enum values in C++. In C# I'd | the values together e.g.
r = VALUE1; r = r|VALUE2;
doing this then allows me to check the enum later to see what values it matches, an example of this would possibly be a validtion routing that returns a single enum value but that value could contain multiple values indicating multiple failures. I've been googling and found code examples that show enum's being combined in the same way, but when I try to do it the compiler gives me the following error: error C2676: binary '|=' : 'Contract::ValidInput' does not define this operator or a conversion to a type acceptable to the predefined operator I've looked at operator overloading but to be honest overloading the | operator is beyond my C++ skills, which are limited at best. Can anybody help me out whilst I still have some hair? -
Something like this doesn't work?
enum TestEnum
{
Value1 = 0x00000001,
Value2 = 0x00000002,
Value3 = 0x00000004,
Value4 = 0x00000008,
Value5 = 0x00000010,
Value6 = 0x00000020,
Value7 = 0x00000040,
Value7 = 0x00000080
};int r = Value1;
r |= Value2;// or
int r = static_cast<int>(Value1);
r |= static_cast<int>(Value2);//*edit* or
TestEnum r = Value1;
r = static_cast<TestEnum>(r | Value2);-- modified at 11:18 Tuesday 8th May, 2007
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Ahh.... need to perform a cast. Thanks for the help
-
Ahh.... need to perform a cast. Thanks for the help
Lowest of the Low wrote:
need to perform a cast
If you look up the enum keyword (and toxxct's article I believe) it explains how enums can be promoted to int (and ARE promoted implicitly when you do arithmetic/logical ops with them). But, to assign an int to an enum, a cast is required. The second example I posted was optional casts - since the result was being assigned to an int variable anyway. The third exaple shows where the cast is required. Cheers! Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder