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 8566071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:34:21+00:00 2026-06-11T17:34:21+00:00

Suppose multiple comparations are required at once: enum usermode { active, standingby, inactive, dead,

  • 0

Suppose multiple comparations are required at once:

enum usermode
{
    active,
    standingby,
    inactive,
    dead,
    // many other modes....
};
class A
{
public:
    usermode mode;
};

function inherited pointer to class A (ptr points to A)

Method A:

if( ptr->mode == active || ptr->mode == standingby || ptr->mode == inactive || ptr->mode == dead ...//etc )
{
    //do something
}

Method B:

usermode cmpmode = ptr->mode;
if( cmpmode == active || cmpmode == standingby || cmpmode == inactive || cmpmode == dead ...//etc )
{
    //do something
}

Is it a good practice to do so?

  • 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-11T17:34:23+00:00Added an answer on June 11, 2026 at 5:34 pm

    In this case it simply cuts down on the amount of characters in the if statement expression, so whatever works and is most readable.

    However, in the case of checking the return value of a function, I would stuff it into a variable and check that, i.e.,

    auto ret = SomePossiblyExpensiveOperation();
    if(ret != whatever && ret < something) {
        //...
    }
    

    Taking your if statement at face value, I would do this:

    switch(ptr->mode) {
        case active:
        case standingby:
        case inactive:
        case dead:
            ptr->whatever();
            break;
        // other cases
        default:
            // something else
            break:
    }
    

    If this is a common check, you may want to make a special case for it, i.e.,

    usermode any_mode = active | standingby | inactive | dead;
    
    //...
    
    if(ptr->mode & any_mode) {
        // whatever...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose multiple Modal Windows shown above each other. All of those have ShowInTaskbar =
Suppose you are creating a class with multiple .cpp files (which each contain the
Suppose I have a Java class with multiple constructors: class Base { Base(int arg1)
In Windows, suppose you have multiple windows (HWNDs) of the same window class open.
Suppose I have singleton class that acts as a data cache. Multiple threads will
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
Suppose a check constraint (involving multiple columns) with the not for replication option was
Suppose I'm accessing a DataTable from multiple threads. If I want to access a
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,
Suppose I create a Windows Azure application that consists of multiple instances talking to

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.