C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 11)
11.
Why reference is not same as a pointer?
A reference can never be null.
A reference once established cannot be changed.
Reference doesn't need an explicit dereferencing mechanism.
All of the above.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 1 of 2.

Chandan said:   1 decade ago
References always stores the address of objects so it can not be null and it can not be changed due to only one reference is possible to one object.

Dinesh kumar said:   1 decade ago
cout is an object of class ostream that represents the standard output stream. It corresponds to the cstdio stream stdout.

Nusrat jan said:   1 decade ago
What is the difference between reference and pointer?

Vaibhav said:   1 decade ago
A pointer can have Null value ,int *p=Null means, it is not having any address to point . while reference can't have &p=Null(not allowed).

2- address can be changed in case of pointer while in case of reference it can't b.

int a=10;
int *p =&a,
int b=30;

int *p=&b; valid now, but in reference it's not possible.

Joshua said:   1 decade ago
Okay, so there's this idea that changing a reference is not possible. Somebody please explain this because, as I have written a program that does it, it is very clearly possible.

classA apples(10);
classA bananas(0);
classA &ref = apples;
ref = bananas;
std::cout << ref.num;


That code will print "0". This means that I have created a reference and assigned apples to it, whose member variable num = 10. I then reassign bananas to ref, and now it is a reference to bananas.

What am I missing here? I've seen 3 sources so far that say this isn't possible when.. it is.

Sachchidanand kumar said:   1 decade ago
What is difference between call by address and call by reference?

Badal Singh said:   10 years ago
In case of call by reference method, actual parameters are normal variables and formal parameters are reference variables whereas in case of call by address, actual parameters are addresses of normal variables and formal parameters are pointer variables.

Gitika said:   9 years ago
Answer to @Joshua.

When your assigning ref=bananas you are not creating a reference but when you are assigning it to apple then it create reference any change to apple will cause change to reference but any change to banana after assigning will not change reference.

Check it out with simple code:

int n=10,r=20;
int &a=n;
n++;
cout<<&a<<" "<< a<<endl;
a=r;
r++;
cout<<&a<<" " <<a;

Now check output you will understand difference.

Ajay said:   9 years ago
Reference doesn't need an explicit dereferencing mechanism. Why?

Ritika said:   8 years ago
Please post an answer to @Ajay's question. Even I've got the same doubt.


Post your comments here:

Your comments will be displayed after verification.