468,457 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

old question: difference between pointer and reference

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

Aug 1 '06 #1
14 6887
Vols wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #2
Your answer doesn't make sense.

Vol.
Victor Bazarov wrote:
Vols wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #3
Vols wrote:
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.
>
Vol.
Victor Bazarov wrote:
>Vols wrote:
>>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #4
according to the following
http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
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

On 1 Aug 2006 08:46:23 -0700, "Vols" <vo********@gmail.comwrote:
>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
Aug 1 '06 #5
Vols wrote:
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.

Aug 1 '06 #6
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;
}

--

Frederick Gotham
Aug 1 '06 #7
Vols wrote:
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.

Aug 1 '06 #8

"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Vols wrote:
>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.
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
Aug 1 '06 #9
Howard wrote:
"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>Vols wrote:
>>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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #10

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ea**********@news.datemas.de...
Howard wrote:
>"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
>>Vols wrote:
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.

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.
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


Aug 1 '06 #11
Howard wrote:
True. I probably should have left out "simply" in my answer.
Or you should have simply left out your clueless answer.

Aug 1 '06 #12

"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Howard wrote:
>True. I probably should have left out "simply" in my answer.

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

Aug 1 '06 #13

Howard wrote:
"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Vols wrote:
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.

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?

Aug 1 '06 #14

"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
Howard wrote:
>"Kaz Kylheku" <kk******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
Vols wrote:
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.

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.
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 ???).
>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?
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


Aug 1 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by CoolPint | last post: by
39 posts views Thread by TonyJeffs | last post: by
4 posts views Thread by z_learning_tester | last post: by
16 posts views Thread by Duncan Mole | last post: by
4 posts views Thread by Diffident | last post: by
20 posts views Thread by tshad | last post: by
30 posts views Thread by Logos | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.