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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:04:06+00:00 2026-05-30T15:04:06+00:00

The project I’m working on has a need to represent many different hardware registers

  • 0

The project I’m working on has a need to represent many different hardware registers to our users as class objects. I’m nearly done on my object model, but I’m at one hurdle that I’m not sure how to surpass yet. These registers are exposed using structures. All of these register structures will inherit from a base struct which will provide useful things like how to get the particular register value from memory and store it to the bit field.

Now, the nice thing is that all of these registers are nicely aligned DWORDs. So, my code is something like this:

struct Base {
    DWORD offset;
    Driver* pToDrv;

    // methods to set/get values to the registers using the driver pointer
    void SetRegister(DWORD val);
};


struct Register1 : public Base {
    DWORD bit0           : 1;
    DWORD bit1           : 1;
    // etc. until 32 bits
    DWORD bit31          : 1;

    // getters/setters for the individual bits

    void SetFullRegister(DWORD val) {
        *(reinterpret_cast<DWORD*>(this)) = val; // uh-oh!  This doesn't point to bitfield
        SetRegister(reinterpret_cast<DWORD*>(this)); // same thing here
    }
};

I’ve verified with a simpler approach in a smaller program that this points to the Base instance even when used in the derived object’s methods. How do I ensure that this points to the start of the derived object for the operations I’m trying to do?

I’ve tried the following approach:

*(reinterpret_cast<DWORD*>(this + sizeof(Derv))) = val;

Although nothing bad seemingly happened, the net result was that nothing at all happened. I know this isn’t true because the value was assigned somewhere but, cringe, I’ve no idea where. This obviously isn’t working nor is it a safe approach.

Please let me know if I’ve not adequately described my problem.

Thanks,
Andy

  • 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-30T15:04:07+00:00Added an answer on May 30, 2026 at 3:04 pm

    Your object model doesn’t represent the content of the register correctly.

    Your register has an offset, and a driver, and content. The latter may have a different layout for each type of register, so can’t be inherited but should be defined on a per-register basis.

    A nested class is a proper means to do that.

    struct Register1 : public Base {
        struct Content {
          DWORD bit0           : 1;
          DWORD bit1           : 1;
          // etc. until 32 bits
          DWORD bit31          : 1;
        } content;
    
        // getters/setters for the individual bits
    
        void SetFullRegister(DWORD val) {
            *(reinterpret_cast<DWORD*>(&content)) = val; // This points to bitfield
            SetRegister(reinterpret_cast<DWORD*>(&content)); // same thing here
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our project is using many static libraries to build the application. How can we
The project I am currently working on has, in the checkout, an option to
The project I'm working on has an Oracle backend, and we use SQL Navigator
Project layout: /project_a /shared /project_b /shared /shared project_a and project_b both need to contain
A project I'm working on at the moment involves refactoring a C# Com Object
My project has both client and server components in the same solution file. I
The Project's Web section (under project properties in VS2008) has a list of debuggers:
Project Requirements: I need to build a web service that will receive chunks of
Project CompetitionServer.exe raised exception class ESQLiteException with message 'Error executing SQL. Error [1]: SQL
Project I'm working on uses jQuery. I have a series of Ajax calls being

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.