private access specifier nature

Can someone tell me what the following does and what does it show about the nature of the private access specifier??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

class thing{
    public:
        int value1 = 5;
        void display(){cout << value2 << endl;}
    private:
        int value2 = 67;
    
};



int main()
{
    
    thing a;
    cout << *(&a.value1 + 1) << endl;
    a.display();
    cin >> *(&a.value1 + 1);
    a.display();
    
    cout<<"Hello World";

    return 0;
}
Last edited on
> Can someone tell me what the following does
It could do anything - because the code is broken.

It makes very specific assumptions about how your compiler chose to layout the innards of your class.

It's like tunnelling into a bank vault.
Sometimes you get lucky and find yourself in the middle of the vault.
Or perhaps you find yourself in the middle of the police station.

> and what does it show about the nature of the private access specifier??
Nothing - because the code is broken.
what does it show about the nature of the private access specifier?

The "liars, cheats, pickpockets, and thieves" do not use C++?

For more examples: http://www.gotw.ca/gotw/076.htm
what does it show about the nature of the private access specifier??


If this is meant to be a simple answer, as for a question in class or some such, what it shows is that the private access specifier is a message to the compiler to not let you write code that specifically access private variables from other classes or non-friend functions, but at run-time you can do whatever you like with any memory you can reach into - private has no meaning at run-time.
Topic archived. No new replies allowed.