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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:10:02+00:00 2026-05-23T11:10:02+00:00

The existing code: typedef unsigned int uint; Class A { union xReg { uint

  • 0

The existing code:

typedef unsigned int uint;
Class A
{
union xReg
    {
        uint allX;
        struct
        {
            uint x3      : 9;
            uint x2      : 9;
            uint x1      : 14;
        }__attribute__((__packed__)) __attribute__((aligned(4)));
    };
};

My requirement:
Now, I need to derive a class from A, and and in the derived class, the bit sizes of x1, x2 and x3 has to change.

How do i do this ?
Thanks for your help !

EDIT

I have a class (lets say A) with approx. 7-8 unions (each representing HW register), and around 20 (approx.) functions. Most of these functions create instances of these unions, and use the bits (x1, x2, x3 etc in my example).

Now, my requirement is to add code for a new hardware which has 95% of functionality same. The changes include the change in register bit sizes, and some functionality change. So, among 20 functions, atleast 5 functions I need to change to change the implementation. This is the reason for me to select inheritance and override these functions.

The rest 15 functions, only change is the bit size changes. So, I dont want to override these functions, but use the base class ones. But, the bit sizes of the registers (unions) should change. How do I do that?

  • 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-23T11:10:03+00:00Added an answer on May 23, 2026 at 11:10 am

    You cannot change a bit-field length in a derived class in C++.

    What you could try, however, is parametrize your class with the bit_field lengths.

    template <size_t N1, size_t N2, size_t N3 = 32 - N1 - N2>
    struct myStruct
    {
        uint bitField1 : N1;
        uint bitField2 : N2;
        uint bitField3 : N3;
    };
    

    Now you can instantiate the struct with any N1, N2, N3 you wish, for example:

    myStruct<9, 9> s;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

existing code which I cannot modify, here expand class is a just a functionality
I'm converting existing code to Spring 3 JDBC. I've put it into a class
The offending code: template<typename T> class SharedObject { public: typedef boost::intrusive_ptr<T> Pointer; typedef boost::intrusive_ptr<T
Existing code is using an asp:repeater to build an HTML table. (emphasis on the
Reading existing code at work, I wondered how come this could work. I have
In some existing code there is a test to see if the user is
Is there any existing code in Apache HttpClient or in the servlet API to
Does anyone know of existing code that lets you draw fully justified text in
I have some existing code that retrieves data from a database using ADO.NET that
In the existing code I am working with I have found the money transfer

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.