C++ Programming - References - Discussion

Discussion Forum : References - General Questions (Q.No. 2)
2.
Which of the following statements is correct?
  1. Once a reference variable has been defined to refer to a particular variable it can refer to any other variable.
  2. A reference is not a constant pointer.
Only 1 is correct.
Only 2 is correct.
Both 1 and 2 are correct.
Both 1 and 2 are incorrect.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 1 of 2.

Vedi G said:   5 years ago
@Anu.

Option 4 is correct because reference cannot have its own address. That's why its is not a constant pointer.

One time it hold one variable address only.

Abhijeet said:   5 years ago
#include<iostream>
using namespace std;
int main()
{
int x = 10;
const int const *ptr=&x;
int& ref = x;
ref = 20;
cout<<"before change ref = "<<ref<<endl;
int y=40;
ref=y;
cout<<"after change ref = "<<ref<<endl;
cout<<"but it actually change x and not change only ref i.e.x = "<<x<<endl;
cout<<"add of x= "<<&x<<endl;
cout<<"add of ref = "<<&ref<<endl;
cout<<"add of ptr = "<<&ptr<<endl;
cout<<"ptr = "<<ptr<<endl;


getchar();
return 0;
}

As you can see,

A reference is not a constant pointer.
as pointer has its own address and ref does not.

Prashant said:   6 years ago
When we are creating a reference to a variable we are actually giving it a different name so logically now ref and x are the same so whenever you are modifying ref you are modifying x and not changing what is ref referring to so the line.

ref=20 sets x also to 20 and line.
ref=y sets x equal to the value of y,
ref=y does not mean that ref is now a reference to y,
you can try printing x it will give you 40.

Therefore option 1 is incorrect.

Harsha said:   7 years ago
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout<<ref;
int y=40;
ref=y;
cout<<ref;
return 0;
}

Here reference is referred to another variable 'y' after referring 'x'. How 1st option is correct?

Pooja said:   7 years ago
A is the correct answer.

Anu said:   8 years ago
I think the option 1 is correct.

Bhasker Bamsiya said:   8 years ago
The Answer mentioned is wrong.

A reference variable can't refer to any other variable once it has been defined to refer to a particular variable.

The correct answer should be option [B].
(1)

Daniel Sandor said:   9 years ago
"The C++ standard does not force compilers to implement references using.

Daniel Sandor said:   9 years ago
"The C++ standard does not force compilers to implement references using pointer.

AKAK said:   9 years ago
- Pointer has its own address. Reference (const or not) does not.

- Const pointer can be NULL. Reference can not.

- You can test for NULL pointer being returned by a function. You cannot test for NULL-reference, even if object being referenced went out of scope.

- You need to deference the pointer to access it's data, there is no deference a reference.

All that makes references conceptually and practically different from const pointers, even though under the hood they can be implemented as the same mechanism by a compiler.


Post your comments here:

Your comments will be displayed after verification.