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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:55:32+00:00 2026-06-13T10:55:32+00:00

Consider the following code: enum TestEnum { TEST_ENUM_5 = 5 }; class Test {

  • 0

Consider the following code:

enum TestEnum
{
   TEST_ENUM_5 = 5
};

class Test
{
public:
   Test() { mType = TEST_ENUM_5; mVal = 1; }
   TestEnum& type() { return (TestEnum&)mType; }

private:
   uint16_t mType;
   uint16_t mVal;
};

int main( int argc, const char* argv[] )
{
   Test test;
   assert( test.type() == TEST_ENUM_5 );
}

The program compiles fine with MSVC 2010 – no errors or warnings. But the assertion fails – the value returned is not 5 but rather 0x00010005.

In other words the value of the returned enumeration is interpreted as a 4 byte value – including the contents of the following short. I can see why the compiler is doing this, the reference is a ref to the address of mType, and a register is being loaded with the next 4 bytes.

But is this correct behavior for the compiler?

Shouldn’t it know that TestEnum& is a reference to a 16-bit quantity? Or if it doesn’t want to do that, shouldn’t it warn?

That aside, what I’d like to do is store a short enumeration into a 16 bit value, and have method that returns a reference to it, that is typed as that typedef. That is logically what I want is an interface that allows me to:

test.type() = TEST_ENUM_5;

And have it know at compile time that only values from TestEnum are expected. For that matter I want users of the class when they read it to know TestEnum values are expected here.

  • 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-13T10:55:33+00:00Added an answer on June 13, 2026 at 10:55 am

    You say that the enum has to be two bytes, and that the return type of type() has to be an enum reference.

    C++11 has a feature whereby you can specify the underlying type of an enumeration:

    enum TestEnum : unsigned short
    {
       TEST_ENUM_5 = 5
    };
    
       TestEnum& type() { return mType; }
    private:
       TestEnum mType;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;
consider the following code: public class SynchronizedCounter extends Thread { private int c =
Consider the following code: public enum SomeCode { NIF = 0 ,NIE = 1
Consider the following code: class A { private $a; function f() { A::a =
Consider the following code: [DataContract] class QObject { public QObject() { } [DataMember(Name =
Please consider the following code: class Student { } enum StudentType { } static
Consider the following Qt code: class Foo : public QObject { Q_OBJECT Q_ENUMS(E) Q_PROPERTY(E
Consider the following: namespace MyNamespace{ class MyClass { public: // Public area private: //
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
please consider following code #include <iostream> using namespace std; class Digit { private: int

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.