Isn't reference a const pointer?

Well, Ideally, once initialized, a reference cannot be made refer to another variable because it, the reference is treated as a const pointer.
I am a bit confused because doing this thing works properly in my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int i=10,j=20;

int& r = i;

cout << "i is\t"<<i<<endl;
cout <<"r is\t"<<r<<endl;
cout << endl;``
//Reassign. No error!

r=&j;

cout<<" i is\t"<<i<<endl;
cout<<"r is\t"<<r<<endl;



i is   10
r is   10

i is   20
r is   20


Please help :) Thanks in advance
nonsense. I don't belive what you have posted.
You are sadly mistaken.
Last edited on
Yeah that shouldn't work. Unless you're using a the spastic compiler 4000;
@guestgulkan - Not sure what exactly you don't believe? My program running successfully part or reference is treated as a const pointer part?

@Lachlan - I am using cygwin CYGWIN_NT-6.1 version.
> once initialized, a reference cannot be made refer to another variable

Yes.

> because it, the reference is treated as a const pointer.

No. The reference is treated as another identifier (an alias) that refers to the object it was initialized to refer to.
1
2
int i = 10, j =- 20 ;
int& r = i ;

Both i and r refer to the same int object. Throughout the life of i, i will continue to refer to the same int object. Throughout the life of r, r will also continue to refer to the same int object that i referes to.

Therefore, these two lines are equivalent:
1
2
i = &j ; // error: invalid conversion from 'int*' to 'int'
r = &j ; // error: invalid conversion from 'int*' to 'int' 


1
2
3
4
5
6
7
int main()
{
    int i = 10, j = 20 ;
    int& r = i ;
    i = &j ;
    r = &j ;
}


> g++ --std=c++11 -c test.cc
test.cc: In function 'int main()':
test.cc:5:10: error: invalid conversion from 'int*' to 'int' [-fpermissive]
test.cc:6:10: error: invalid conversion from 'int*' to 'int' [-fpermissive]

> g++ -fpermissive -c test.cc
test.cc: In function 'int main()':
test.cc:5:10: warning: invalid conversion from 'int*' to 'int' [-fpermissive]
test.cc:6:10: warning: invalid conversion from 'int*' to 'int' [-fpermissive]


With -fpermissive, the (possibly narrowed or widened) numeric value of an address will be assigned to i. The effect would be the same as writing :
1
2
i = reinterpret_cast<int>(&j) ;
r = reinterpret_cast<int>(&j) ;

Last edited on
My apologies. After declaration, I was actually trying
1
2
3
4
5
r=j
//instead of

r=&j


In first case, it still manipulates the value of both i and r. I guess values can be manipulated but second case is not valid, since r being a const pointer.

here is the program
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
$ cat referece.cpp
#include<iostream>
using namespace std;

int main()
{
  int i=10, j=20;

  int& r =i;

  cout << "value of i is\n"<<i<<endl;
  cout << "value of r is\n"<<r<<endl;

 cout <<" address of i is\n"<<&i<<endl;
 cout << "address of j is\n"<<&j<<endl;
 cout << "address of r is\n"<<&r<<endl;

  r=j;

 cout << "value of i is\n"<<i<<endl;
 cout << "value of r is\n"<<r<<endl;
 cout <<" address of i is\n"<<&i<<endl;
 cout << "address of j is\n"<<&j<<endl;
 cout << "address of r is\n"<<&r<<endl;

 return 0;
}

[output]
$ ./a.exe
value of i is
10
value of r is
10
 address of i is
0x22cd18
address of j is
0x22cd14
address of r is
0x22cd18
value of i is
20
value of r is
20
 address of i is
0x22cd18
address of j is
0x22cd14
address of r is
0x22cd18 //address of r remains same as that of i. Only value is manipulated.

[/output].

Thanks JLBorges for clarification :)


Topic archived. No new replies allowed.