small dereferencing problem

Hello everyone, my first question is that in the code that follows, I think I understand why &a and &b have the output, but I have no idea how b, a and a++ all share the same output of 10, considering that if I duplicate the last line the second "cout << a++ <<endl;" actually outputs 11. And question number 2 is are constructors considered class member functions(methods)?


#include <iostream>
using namespace std;

int main ()
{
int a = 10;
int& b = a;


cout << &b << endl;
a++;
cout << &a << endl;
b = b - 1;
cout << b << endl;
cout << a << endl;
cout << a++ << endl;

return 0;
}


thanks everyone in advance for replies =)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main ()
{
int a = 10;
int& b = a;//b's address is = to a's address


cout << &b << endl;//prints memory address 
a++;//makes a = 11
cout << &a << endl;//notethe address is the same as b's
b = b - 1;//makes a = 10
cout << b << endl;
cout << a << endl;
cout << a++ << endl;//makes a = 11 but not untill after it is used (output)
return 0;
}
Last edited on
ahhhh makes sense thank you very much =D
Topic archived. No new replies allowed.