0
class thing {
public:
int value1 = 5;
void display(){cout << value2 << endl;}
private:
int value2 = 77;
};
int main()
{
thing A;
cout << A.value1 << endl;
cout << A.value2 << endl;
cout << *(&A.value1 + 1) << endl;
A.display();
cin >>  *(&A.value1 + 1);
A.display();
return 0;
}

Here lies the class, and i don't really understand the difference between
*(&A.value1 + 1) and *(&A.value1 + 1)

  • 2
    You asked the difference between `*(&A.value1 + 1)` and `*(&A.value1 + 1)`. Those are both the same, so there is no difference... 99% sure they're both undefined behavior, so they might invoke nasal demons, but they're clearly the same thing. – ShadowRanger Sep 30 '20 at 00:25
  • 1
    There is no difference. `&A.value1` gives the address of where `A.value1` is stored. Then you look at the value in the next array location which is not allowed because `A.value1` isn't an array. After that the compiler is free to do anything it wants including doing what you expect or something you didn't expect. – Jerry Jeremiah Sep 30 '20 at 00:25
  • Maybe you need to explain why you think there is a difference. What output did the program create that made you wonder? I'm just asking because the program doesn't even compile for me. – Jerry Jeremiah Sep 30 '20 at 00:30
  • As an example of undefined behaviour doing something you didn't expect, have a look at https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=633 – Jerry Jeremiah Sep 30 '20 at 00:32
  • you are right Jerry Jeremiah, the output is just same, the output on my side is 5 77 77, which are the values of value1 and value2, then I input a whatever number and A.display() display the number i just input. – flying shark Sep 30 '20 at 00:36

0 Answers0