reference issues

We cannot apply arithmetic operations on a reference then how cum the follow code runs without error...shudn't n++ give an error?

1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
int main()
{
int a = 20;
int &n = a;
n=a++;
a=n++;
cout<<a;
}
A reference is an alias for another object. You aren't applying arithmetic operations to the reference. You are applying arithmetic operations to the aliased object.

Your code, by the way, results in undefined behavior on lines 7 and 8.
@mehak this is what you are doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
        int a = 5;
        int *n = &a;            

        *n = ++a;
        a = ++(*n);

        cout << *n;

        cout << endl;
        return 0;
}
Topic archived. No new replies allowed.