M
man
can any one please tell me what is the diff between pointer and
reference.....and which one is better to use ....and why???????
reference.....and which one is better to use ....and why???????
man said:can any one please tell me what is the diff between pointer and
reference.....and which one is better to use ....and why???????
ryadav said:Adding to Oliver post, refferences should always be initialised,
pointer need not be.
they again have many adavantages and disadvantages...more given in book
can any one please tell me what is the diff between pointer and
reference.....and which one is better to use ....and why???????
This is because a reference is an alias( as already mentioned ). So
this is not a different point that you are putting down.
Greg said:Actually, it is a big difference. The concept of a reference
being an alias did not have to mean that it had to be initialized,
however, it was determined that for C++ that having it that way
was a good idea. Also, there are other ways in C++ to establish
aliases, and they do not have such a syntactic demand, perhaps
unfortunately.
Al said:Why do you often use constant references? What is the point of doing
that?
I heard of being faster because of not tranfering and copying the
object when called as argument... more details?
ex: T(const T& r): a(r.a), b(r.b) {} instead of T(T r): a(r.a), b(r.b)
{}
Al said:Why do you often use constant references? What is the point of doing
that?
I heard of being faster because of not tranfering and copying the
object when called as argument... more details?
ex: T(const T& r): a(r.a), b(r.b) {} instead of T(T r): a(r.a), b(r.b)
{}
or many other cases
why is it so important to do it?
Oliver said:Hi,
a reference is like an alias. A different name for the same thing (i.e.
object or function).
A Pointer points to the physical address of a
variable/object.
Kaz Kylheku said:A reference isn't a name.
It's a specially attributed variable (local, global or member), which
is implemented as a hidden pointer to the real object.
Where does that standard say that? A reference may not even exist as aSo does a reference.
REH said:References are not variables. They cannot be changes, nor does the standard
require that they be implemented in a particular way.
Where does that standard say that? A reference may not even exist as a
physical entity (i.e., not exist in memory) in a compiled program.
Kaz Kylheku said:Indeed, a variable binding cannot be changed. You can change what it
contains, but not the binding itself!
When you execute the defintion
{
int x = 3;
}
x gets bound to a storage object which holds an int. You can't, in the
same context, rebind x to a different object.
A reference is the same way. It's a named binding to some object which
holds a value. The only difference is that the whereas x above gets a
fresh location in automatic storage, a reference variable gets a
binding to an existing object that could be in anywhere.
A variable is just an association between a name and a storage
location.
Something can be a physical entity without existing in memory. All
elements of the processing system are physical; registers are physical.
What you are talking about is an optimization. In the abstract
semantics, a reference locates an object. Locating objects is done by
means of their address. In the actual semantics, perhaps the location
can be inferred statically, and so the reference need not require any
run-time storage for its binding.
A reference isn't a name.
It's a specially attributed variable (local, global or member),
which
is implemented as a hidden pointer to the real object.
A reference can be bound to an object which has no name.
int &x = * new int;
delete &x;
You can also have const references, which can be bound to non-lvalue
expressions.
So not only does the subject of a reference not have to
have a name, it doesn't even have to be a first-class object:
void take_ref(const int &x);
take_ref(2 + 2);
If x above is ``another name'' for something, what is the original name
of that thing?
To allow the above, C++ postulates the existence of a
temporary object that holds the result of 2 + 2.
So does a reference.
Greg said:Not in the sense of a name as in an identifier. But it certainly
does "names" something, akin to say how *p might name something.
This tends to dispute your previous claim.
Anyway, it need not only be local/global/member.
Which _may be_ implemented as a pointer.
As you note, it may be implemented as such, but as a concept,
it's certainly more along the lines of an alias -- even if the
original entity did not have a formal name.
You could of course
make an argument that even in int i = 99; that i at some point(!)
"points" to/at the physical address along the line of a file in a
filesystem being a link to its storage even if it's the first link.
REH said:No, I am not talking about optimization. In this:
int x = 1;
int& y = x;
y may be completely "ignored" by the compiler, replacing it with x. No
pointer, no optimization removing said pointer.
You still have not shown where in the standard it says a reference must be
implemented as a pointer.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.