Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Combine enum values

Combine enum values

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialcsharpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Gloyn
    wrote on last edited by
    #1

    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?

    M H T 3 Replies Last reply
    0
    • N Nathan Gloyn

      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?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • N Nathan Gloyn

        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?

        H Offline
        H Offline
        Haiying
        wrote on last edited by
        #3

        try int r = 1; //if contains value1 r |= value1; //if contains value2 r |= value2; make sure r is an int, value1 and value 2 are defined in enum. If value1 is the second item, value 2 is third item in enum, you should get r=7 (1+2+4)as a total.

        1 Reply Last reply
        0
        • N Nathan Gloyn

          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?

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          have a read at this[^] little article


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • M Mark Salsbery

            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

            N Offline
            N Offline
            Nathan Gloyn
            wrote on last edited by
            #5

            Ahh.... need to perform a cast. Thanks for the help

            M 1 Reply Last reply
            0
            • N Nathan Gloyn

              Ahh.... need to perform a cast. Thanks for the help

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups