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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:47:39+00:00 2026-05-25T10:47:39+00:00

Is there a way to overload a cast operator to convert between two enums?

  • 0

Is there a way to overload a cast operator to convert between two enums?

In my code I have

enum devStatus
{
    NOT_OPERATING,
    INITIALISING,
    DEGRADED,
    NORMAL
};

enum dataStatus
{
    UNAVAILABLE = 1,
    DEGRADED,
    NORMAL
}

where NOT_OPERATING and INITIALISING map to UNAVAILABLE; DEGRADED and NORMAL map straight across. These are fixed by external interfaces.

I am looking for a way to convert between devStatus and dataStatus and would like to be able to write something like:

devStatus devSts;
getDeviceStatus(&devSts);
dataStatus dataSts = (dataStatus)devSts;

I know that if these were classes, I could write devStatus::operator dataStatus() to do this. Is there a way of doing this for a enums?

I could just have a free function
dataStatus devStatus2dataStatus(const devStatus& desSts)

  • 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-05-25T10:47:40+00:00Added an answer on May 25, 2026 at 10:47 am

    In C++, conversion operators can only be declared within class, struct, and union declarations. They cannot be declared outside the type (like operator+, for example). Enum type declarations do not support instance members, so you will need to go with the conversion function. Doing so will also make the calling code clearer. The following example demonstrates this, using custom namespaces to scope the enumerations and conversion functions:

    namespace TheirEnum {
       enum Type {
          Value1,
          Value2
       };
    }
    namespace MyEnum {
       enum Type {
          Value1,
          Value2
       };
    
       TheirEnum::Type ToTheirEnum (Type e) {
          switch (e) {
             case Value1: return TheirEnum::Value1;
             case Value2: return TheirEnum::Value2;
             default: throw std::invalid_argument("e");
          }
       }
    }
    
    // usage:
    TheirEnum::Type e = MyEnum::ToTheirEnum(MyEnum::Value1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to overload, say the >> operator for function composition? The
Is there a way to overload the << operator, as a class member, to
Possible Duplicate: Operator Overloading in PHP Is there a way to overload the =
Is there a way I can call an operator overload in C++ and call
Is there a way to overload the event += and -= operators in C#?
Is there a way of forcing the C# compiler to ignore missing operator overloads
Is there way in next piece of code to only get the first record?
For convenience, I'd like to be able to cast between two types defined in
Is there a better way to overload a macro like this? I need a
Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In

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.