Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:38:46+00:00 2026-06-13T09:38:46+00:00

let’s say I have this: enum E{ a, b, c, total } class A

  • 0

let’s say I have this:

enum E{ 
a,
b,
c,
total
}

class A {
char mask; // supposed to contains combinations of values of the enum, like a or c, etc
}

Is there a decent solution to build the object A in a user-friendly way?
For instance I could do:

A(E e) {
mask = 1 << e;
}

but this will only work if you want the mask to be made from only 1 element of the enum
Ideally the user would be able to do something like :

A* a = new A(a | c)

and this would automatically create

mask = 1 << a | 1 << c;

Any idea on how to do this correctly?
thanks

edit

sadly I have no control over the initial enum and the values are increasing 1 by 1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:38:47+00:00Added an answer on June 13, 2026 at 9:38 am

    Well, there’s the simple way and the ugly way.

    The simple way is to define the bits as non-overlapping to being with, e.g.:

    enum E
    { 
       a = 1 << 0,
       b = 1 << 1,
       c = 1 << 2,
       total = 1 << 3 // or a | b | c, not sure what the intent was
    };
    

    The ugly way:

    #define MAKEA(x) (new A((E_helper() | x).get()))
    

    where E_helper is a class that overrides operator|(enum E).

    class E_helper
    {
        unsigned accum;
    public:
        explicit E_helper(unsigned initial = 0) : accum(initial) {}
        unsigned get() const { return accum; }
        E_helper operator|(enum E e) const { return E_helper(accum | (1 << (unsigned)e)); }
    };
    

    And then your user can say

    A* p = MAKEA(a | c);
    

    which expands to

    A* p = (new A((A_helper() | a | c).get());
    

    which causes this chain of events

    A_helper.operator|(a).operator|(c).get()
    

    Better yet, move the .get() call inside A’s constructor, which then takes an argument of type A_helper. This will let you catch the case where the user forgot to use the MAKEA macro.

    Be warned, however, that the ugly way exposes counter-intuitive behavior. For example, A* p = MAKEA(a | c); is different from A* p = MAKEA( (a | c) ); and char mask = a | c; A* p = MAKEA(mask);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say you have a class called Customer, which contains the following fields: UserName
Let me explain best with an example. Say you have node class that can
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have some text as follows: do this, do that, then this,
Let's say I have the following entity: public class Store { public List<Product> Products

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.