C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 11)
11.
Why reference is not same as a pointer?
Discussion:
13 comments Page 1 of 2.
Pallavi said:
7 years ago
You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time.
A reference must be initialized when it is created. Pointers can be initialized at any time.
Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time.
A reference must be initialized when it is created. Pointers can be initialized at any time.
(2)
Sruthi said:
7 years ago
Reference doesn't need an explicit dereferencing mechanism. Why?
Akash shelke said:
8 years ago
Can pass the parameters inside a main?
Ritika said:
8 years ago
Please post an answer to @Ajay's question. Even I've got the same doubt.
Ajay said:
9 years ago
Reference doesn't need an explicit dereferencing mechanism. Why?
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.
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.
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.
Sachchidanand kumar said:
1 decade ago
What is difference between call by address and call by reference?
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.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers