References vs Pointers?

RobotGymnast's Avatar
Newbie Member
I know somebody posted a thread about this, but that was really only listing in what ways pointers are better than references, but it didn't compare really. It also didn't give situations where either would be useful, when to use what, pros and cons, etc. What are the pros and cons? When should I use which?

Thanks, RobotGymnast
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
You can get anywhere...Just Google it...
0
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
Quote:
Originally Posted by asadullah.ansari View Post
You can get anywhere...Just Google it...
Its interesting that patron of the year gave this reply. Just imagine if we are suppose to get the similar answers from our teachers

Well back to the topic.

Here are the basic differences

1. Reference has to assign to something at the time of decleration, however pointer dont have to.
2. Reference can't be null, but pointer can be so no need to check for null reference just like we do in case of pointers
3. Once reference is assigned it can't be changed to assigned something else. On the other hand pointer can point to different thing after reassingment.

If there is still confusion please feel free to post it.

PS. there are no dumb or stupid questions.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Also you can Check out - Difference between Pointers and Reference in C++
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Quote:
Originally Posted by zamjad View Post
Its interesting that patron of the year gave this reply. Just imagine if we are suppose to get the similar answers from our teachers

Well back to the topic.

Here are the basic differences

1. Reference has to assign to something at the time of decleration, however pointer dont have to.
2. Reference can't be null, but pointer can be so no need to check for null reference just like we do in case of pointers
3. Once reference is assigned it can't be changed to assigned something else. On the other hand pointer can point to different thing after reassingment.

If there is still confusion please feel free to post it.

PS. there are no dumb or stupid questions.
First thing i didn't mean that whatever you mention. Definition of any term(reference & pointer) offcourse you can find anywhere, any basic books...So what i expect that the guy who asked this question, will search and read about it and come up with some doubts, some problem based on it etc, so it's benefit to him only...
To develope a person knowledge go and just read any books based on " How to make teaching style efficient" so that it will boost your learning capacity, thinking capacity etc.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> Its interesting that patron of the year gave this reply. Just imagine if we are suppose to get the similar answers from our teachers

Asadullah's correct, it's a waste of time typing a response to a question that can easily be Googled. The difference between us and your teachers is that they are paid to teach you directly. If you want to pay us the appropriate rate then yes of course we'll teach you directly instead of pointing you to Google.

But while we're giving you our help free of charge you take it on our terms, which are that you Google stuff first and check out the basics, THEN we'll help you with stuff you don't understand.

Also, an essential skill for programmers is that you can find out stuff for yourself. In the course of a programmer's life you will read MANY manuals and you will have to dig, often for hours on end, to find stuff out, and there won't always be someone on hand to spoon-feed you the answer. If you can't find something then there are two problems (1) you need to find something out and (2) you don't know where to look. If you haven't even bothered to check Google then you're going to get a short answer - JFGI.

And it's obvious you haven't checked Google when the question you asked is entered into Google verbatim and the first few hits contain the answer. For example just Google "c++ pointer reference" - the top hit doesn't seem relevant to me but hits 2,3,4,5 and 6 seem directly relevant.

And in fact the TOP hit in Google for "References vs Pointers" is directly relevant.
0
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
I dont mind for whatever your terms and condition are for telling the answer is, but just want to clear at least one thing.

Quote:
Originally Posted by xpi0t0s View Post

And it's obvious you haven't checked Google when the question you asked is entered into Google verbatim and the first few hits contain the answer. For example just Google "c++ pointer reference" - the top hit doesn't seem relevant to me but hits 2,3,4,5 and 6 seem directly relevant.
It was not me who asked this question, so whatever you wanna say; tell it directly to the OP. Instead of "you" it should be OP.
0
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
Quote:
Originally Posted by asadullah.ansari View Post
First thing i didn't mean that whatever you mention. Definition of any term(reference & pointer) offcourse you can find anywhere, any basic books...So what i expect that the guy who asked this question, will search and read about it and come up with some doubts, some problem based on it etc, so it's benefit to him only...
To develope a person knowledge go and just read any books based on " How to make teaching style efficient" so that it will boost your learning capacity, thinking capacity etc.
got your point. But from your this reply

Quote:
Originally Posted by asadullah.ansari View Post
You can get anywhere...Just Google it...
It look very rude to me and probably the OP too. In the forum, we don’t see the face of other, so whatever we judge about others personalities are from their words. You can explain the same thing in little bit polite way just like you did aftermath. I appreciate your detailed reply.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Quote:
Originally Posted by zamjad View Post
I dont mind for whatever your terms and condition are for telling the answer is, but just want to clear at least one thing. It was not me who asked this question, so whatever you wanna say; tell it directly to the OP. Instead of "you" it should be OP.
Sorry, that's a nuance of the English language (which is my first). I said "when", not "because", so the statement does not imply you asked the question, which I knew when I made that comment.
0
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
Now this question is little bit outdates. In new version of C++ also known as C++0x there are two types of references. One is lvalue reference (same as old one) and the new one is rvalue reference.

The rvalue reference is decleared and initilized as

Code:
int iValue = 10;
int&& ref = iValue
Note there are two & signs with rvalue reference not one.

For further information you can read it from the draft of C++ standard document no n2800You can get this document here

http://www.open-std.org/jtc1/sc22/wg...2008/n2800.pdf