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

The Archive Base Latest Questions

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

I’m trying to access and modify the private data member of a class by

  • 0

I’m trying to access and modify the private data member of a class by offset.
AFAIK, first thing is to calculate the offset, then access the member through the offset.
Here is my code.

class Test {  
public:
    int a;  
    int b;
private:  
    int c;
};  
Test test;
cout << *(&test + &Test::b + 1);  // access c

I got an error : “+” operator invalid, the right operand contains “int Test::*” type.

There is a similar post, it printf the offset of abas 1,4, but when try like this:

cout << &Test::a << '\t' << &Test::b;  

I got 1 1.

My question:
1. Why cout got the wrong result?
2. What does Test::*p point to?
3. How to access and modify the Test::* pointer?(Or how to access the private member when doesn’t know the offset?)

  • 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-25T09:47:36+00:00Added an answer on May 25, 2026 at 9:47 am

    This works:

    #include <iostream>
    using namespace std;
    class test {
      public:
          int a;
          int b;
          test () : c(100) {}
          void show () {cout << "c = " << c << endl;}
      private:
          int c;
    };
    
    
    int main (void)
    {
      test x;
      test *ptr;
      ptr = &x;
      ptr++;
      ptr = (test *) ((int *)ptr - 1);
      cout << "c = " << *((int *)ptr) << endl;
      *((int *)ptr) = 200;
      x.show ();
      return 0;
    }
    

    But the offset should be known. In the above ptr is type of test and therefore ptr++ will increment the pointer value to sizeof (test). Because the location of the private member c is at the end and is a type of integer so one integer step is decreased by casting ptr into int * first and then decrementing 1, which now points to the address of c. It is printed first, and next the value of c is modified by first casting ptr to int * and then assigning a value, and then printed.

    It is not guaranteed that it will always get the value of c when you know the position of c as the padding might be different in other cases. Also there is no point accessing a private data member because at the time of the object design approach it was made to be accessed by the member functions and is a tool for organization by providing abstraction. When implementing such a concept in other languages like C, which does not have object oriented features, you can implement such a private – public environment by personally following a convention, but the compiler will not enforce anything if you accessed a “private” where you actually shouldn’t. But in C++ the compiler provides the restriction and stops you from diverting and breaking the object orientated design which you made. At run time all’s in memory and you can access them without any restriction from your code at that time nothings private or public. If you know how to interpret the bytes in the executable you can change anything you like, even the code itself. I do not think this is a good idea to be implemented, as it will make the code unpredictable and unportable and definitely violate the OOD approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.