old question: difference between pointer and reference

V

Vols

If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

Vol
 
V

Victor Bazarov

Vols said:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
"Yes".

I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

See above.

V
 
V

Victor Bazarov

Vols said:
Your answer doesn't make sense.

Don't top post.

And if *you* can't make sense of it, it doens't mean there isn't any.

If you need to form your own opinion about the differences, read the
FAQ, read the archives, search the web for answers. We don't need to
beat this dead horse again.

V
 
S

suchodj

according to the following
http://en.wikipedia.org/wiki/Reference_(C++)
one reads :

C++ references differ from pointers in several essential ways:

* It is not possible to refer to a reference object directly after
it is defined; any occurrence of its name refers directly to the
object it references.
* As a consequence of the first point, neither arithmetic, casts,
nor any other operation can be performed on references except copying
their binding into other references.
* Once a reference is created, it cannot be later made to
reference another object; we say it cannot be reseated. This is often
done with pointers.
* References cannot be null, whereas pointers can; every reference
refers to some object, although it may or may not be valid.
* References cannot be uninitialized. Because it is impossible to
reinitialize a reference, they must be initialized as soon as they are
created. In particular, local and global variables must be initialized
where they are defined, and references which are data members of class
instances must be initialized in the class's constructor.

There is a simple conversion between pointers and references: the
address-of operator (&) will yield a pointer referring to the same
object when applied to a reference, and a reference which is
initialized from the dereference (*) of a pointer value will refer to
the same object as that pointer, where this is possible without
invoking undefined behavior. This equivalence is a reflection of the
typical implementation, which effectively compiles references into
pointers which are implicitly dereferenced at each use.

A consequence of this is that in many implementations, operating on a
variable with automatic or static lifetime through a reference,
although syntactically similar to accessing it directly, can involve
hidden dereference operations that are costly.

Also, because the operations on references are so limited, they are
also much easier to reason about formally than pointers, and harder to
cause errors with. While pointers can be made invalid through a
variety of mechanisms, ranging from carrying a null value to
out-of-bounds arithmetic to illegal casts to producing them from
random integers, a reference only becomes invalid in two cases:

* If it refers to an object with automatic allocation which goes
out of scope,
* If it refers to an object inside a block of dynamic memory which
has been freed.

The first is easy to detect automatically due to static scoping of
variables; the second is more difficult to assure, but it is the only
concern with references, and one suitably addressed by a reasonable
allocation policy.


hope it helps
friendly
jerome
 
T

Thorsten Kiefer

Vols said:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?

Vol

References are syntactical sugar to make the programmers life easier.
In the case of a const-reference as a parameter to a function you can even
pass non-lvalues. With pointers this isn't possible.
 
F

Frederick Gotham

Vols posted:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?
I say " pointer could point to NULL, but there is no null reference",
What is your opinion?


Let's start off with a guinea pig object:

int obj;

I suppose you could say that the following two definitions are equivalent:

(1) int &r = obj;

(2) int *const p = obj;

Except that:

(A) The first one can't be null.

(B) You must dereference the second one to get at the object, e.g.:

int i;

i = r;
i = *p;

A "reference to const" has further functionality in that it can bind to an
R-value:

int const &r = 5; /* OK */

int const *const p = &5;
/* ERROR: Can't take address of an R-value */

You might think that it doesn't make sense to be able to have a reference
to an R-value, but C++ imposes special treatment for "references to const".
It treats the following:

int const &r = 5;

As if you had written:

int const val = 5;
int const &r = val;

Similarly, if you want to bind a reference to the return value of a
function:

SomeType SomeFunc();

int main()
{
SomeType const &r = SomeFunc();
}


Then it's as if you had written:

SomeType SomeFunc();

int main()
{
SomeType const obj( SomeFunc() );

SomeType const &r = obj;
}
 
K

Kaz Kylheku

Vols said:
If the people ask what is the different between pointer and reference,
what is the brief and good answer?

Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value. A pointer is a first-class data type for
representing the storage location of an object.
 
H

Howard

Kaz Kylheku said:
Brief and good answer:

In the context of C++, a reference is a declarative and type casting
gadget which causes an object itself to be propagated rather than a
copy of its value.

What??? What's a "declarative and type casting gadget", and what does a
reference have to do with type casting?

A reference is simply an "alias" for an existing object.

-Howard
 
V

Victor Bazarov

Howard said:
What??? What's a "declarative and type casting gadget", and what
does a reference have to do with type casting?

A reference is simply an "alias" for an existing object.

I think you need to remember that references help using objects
polymorphically.

V
 
H

Howard

Victor Bazarov said:
I think you need to remember that references help using objects
polymorphically.

True. I probably should have left out "simply" in my answer. A good C++
book is undoubtedly the best place for a complete description of references,
(not someone's "brief and good answer").

-Howard
 
H

Howard

Kaz Kylheku said:
Or you should have simply left out your clueless answer.

Instead of a personal attack, you could let us know what you meant by your
own answer. Specifically, what do you mean by: "a declarative and type
casting gadget".

Also, it's not that clear what you meant when you said it "causes an object
itself to be propogated". A reference "refers" to another object. It's an
alias for that object. Propogation implies some sort of movement or
copying.

-Howard
 
K

Kaz Kylheku

Howard said:
What??? What's a "declarative and type casting gadget", and what does a

English lesson:

A gadget is a mechanism; a device. In a programming language, that is
syntax plus semantics. Declarative means having to do with
declarations, and type casting means having to do with type casts.
reference have to do with type casting?

Gee, I don't know! How about:

void func(Base &b)
{
Derived d = dynamic_cast<Derived &>(b);
}

That is a reference cast. Here, the reference isn't used to declare
anything. So if I had said that references were declarative gadget, I
would have missed the type casting use.
A reference is simply an "alias" for an existing object.

You're just parroting an oft repeated statement that only applies to
block scope reference variables. It is misleading to varying degrees if
applied to other situations. Entities which are not object-like can be
attributed as entities, such as function return values.

Also, when would an alias ever be constructed for a NON-existing
object?
 
H

Howard

Kaz Kylheku said:
English lesson:

A gadget is a mechanism; a device. In a programming language, that is
syntax plus semantics. Declarative means having to do with
declarations, and type casting means having to do with type casts.


Gee, I don't know! How about:

void func(Base &b)
{
Derived d = dynamic_cast<Derived &>(b);
}

That is a reference cast. Here, the reference isn't used to declare
anything. So if I had said that references were declarative gadget, I
would have missed the type casting use.

The fact that you can cast something to a reference doesn't make the
reference a "type casting gadget". Your original statement was made to
someone who is a newbie, who didn't understand what a reference was. I
think your explanation was not very helpful in that context. That's why I
asked you to clarify it. I apologize if my reply sounded arrogant (esp. by
using the ???).
You're just parroting an oft repeated statement that only applies to
block scope reference variables. It is misleading to varying degrees if
applied to other situations. Entities which are not object-like can be
attributed as entities, such as function return values.

Also, when would an alias ever be constructed for a NON-existing
object?
Oh, come on!


I don't mind discussing issues here, even if I'm shown to be wrong. But can
we keep this on a professional level?

-Howard
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top