Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: Pointers: What's the point? (http://www.velocityreviews.com/forums/t267673-re-pointers-whats-the-point.html)

foo 06-24-2003 08:07 PM

Re: Pointers: What's the point?
 
Michiel.Salters@cmg.nl (Michiel Salters) wrote in message news:<cefd6cde.0306230254.63b0385c@posting.google. com>...
> "no_sp@m" <no_sp@m> wrote in message news:<3ef5f7a9$1_3@news.unc.edu>...
> > Pointers and references behave exactly the same, except the syntax of
> > how you write them and use them is different.

>
> Wrong. There are a number of differences:
>
> * pointers can be NULL, or not initialized
> * temporaries can be bound to const references only.
> * Pointers are objects and can be memcpy'ed
> * Copy ctors must use references
> * Pointer values can be changed, references stay bound to
> the same object during their lifetime.
> * Pointers support pointer arithmetic.
>
> That's not quite "exactly the same".
>
> > That said, we can rephrase
> > the question, "What's the point of pointers and references?" to just
> > "What's the point of references?" (or, "What's the point of pointers?").

>
> You can't. Some things require pointers, some things require references.
>
> > Answer:
> > 1. Pointers and references allow objects to be stored and passed as
> > parameters by copying just the *address* of the object ("pass by
> > reference"), not the entire object ("pass by value"). They also allow
> > objects to be returned by reference (careful, though: the object must
> > exist outside the method, e.g., as a global variable or as "this").
> > 2. Pointers and references force "automatic updating" (my term). i.e.,
> > If two copies of a pointer exist and the value of one is changed, then
> > the value of the other changes, too. This is useful for changing
> > parameters inside methods, and sharing values between instances of classes.

>
> Untrue. If you change the value of a pointer, it points to a different
> object. You're confusing the value of a pointer and the value it points
> to.
> int a =1; int b=2;
> int ptr = &a;
> // Change the value of ptr
> ptr = &b;
> // Change the value ptr points to
> ++*ptr; // b is now 3
>
> Because references aren't objects they don't *have* a value. They
> only refer/point to values.
>
> > 3. Pointers allow you to declare a space to hold arrays when the size is
> > not known until run time (i.e., you must use the "new" operator).

>
> Precisely, new and delete don't work on references.


Wrong. :-)
You can use new and delete on references.
Example:
int &x = *(new int());
delete &x;

While I agree that you shouldn't use new and delete on references,
that doesn't mean that you can't.

Matthias Reik 06-26-2003 10:34 PM

Re: Pointers: What's the point?
 
Some comments:


>>Wrong. There are a number of differences:
>>
>>* pointers can be NULL, or not initialized

What's the point of not initializing a pointer? How do you know your
pointer is pointing to a real object or into nirvana?

>>* temporaries can be bound to const references only.
>>* Pointers are objects and can be memcpy'ed
>>* Copy ctors must use references

Who said that ctors MUST use references? You can equally well use a
pointer for that... yeah, you should be a litte bit more careful, to not
confuse people, but that't another story :)

>>* Pointer values can be changed, references stay bound to
>> the same object during their lifetime.

Well this makes the assumption, that you are are talking about non-const
pointers

>>* Pointers support pointer arithmetic.
>>
>>That's not quite "exactly the same".
>>
>>
>>>That said, we can rephrase
>>>the question, "What's the point of pointers and references?" to just
>>>"What's the point of references?" (or, "What's the point of pointers?").

>>
>>You can't. Some things require pointers, some things require references.

What is it that would require a reference? I would say it more the
following way: You can do everything with pointers, but some things are
easier and more clear with references.



Victor Bazarov 06-27-2003 09:42 PM

Re: Pointers: What's the point?
 
"Matthias Reik" <matthias.reik@cenara.com> wrote...
> Victor Bazarov wrote:
> > "Matthias Reik" <matthias.reik@cenara.com> wrote...
> >
> >>Some comments:
> >>
> >>
> >>
> >>>>Wrong. There are a number of differences:
> >>>>
> >>>>* pointers can be NULL, or not initialized
> >>>
> >>What's the point of not initializing a pointer?

> >
> >
> > The point is to save time. If you know that the pointer is
> > going to be calculated somehow, why initialise it to anything
> > before that?

> Saving time is a good idea, but often you are just building your own
> traps. It often works fine as long as you are following the standard
> execution path, but (very often) as soon as something unexpected
> happens, your assumtpions will be wrong. Yes, in some cases you
> don't need to do this, but would you consider this being the rule?
> I wouldn't


I didn't say it was THE rule.

> >>How do you know your
> >>pointer is pointing to a real object or into nirvana?

> >
> >
> > You know if you made it point somewhere.

> So you need to keep track whether it's pointing somewhere? Wouldn't
> that be easier if you would init it to NULL


No, it wouldn't. See above. If it's I who is writing the code,
*I* know whether I initialised it or not. If it's somebody else's
pointer coming into my function, you bet your ass I will check it
for being null.

> >>>>* temporaries can be bound to const references only.
> >>>>* Pointers are objects and can be memcpy'ed
> >>>>* Copy ctors must use references
> >>>
> >>Who said that ctors MUST use references?

> >
> >
> > The Standard said that.

> Are you REALLY sure, I don't think so (even so I haven't got the
> standard in reachable distance).


Well, ignorance is a bliss, ain't it? Once you get a copy of the
standard in reachable distance, reach for it and read 12.1/10.

> >>You can equally well use a
> >>pointer for that... yeah, you should be a litte bit more careful, to not
> >>confuse people, but that't another story :)

> >
> >
> > That wouldn't be a copy constructor. It would be just
> > another parametrised one.
> >
> >>>>* Pointer values can be changed, references stay bound to
> >>>> the same object during their lifetime.
> >>>
> >>Well this makes the assumption, that you are are talking about non-const
> >>pointers

> >
> >
> > Feeling like nit-picking today, aren't we?

> no, but talking about something that is seldomly used. What are you
> allowed to changed if you have got:
> fn(Object const* const)?


I am afraid I don't understand the question, even in the context.
There are no non-const references (references to non-const objects
don't count), but there are non-const pointers. _If_ a pointer
is non-const, you can change it. I imagine that had C++ not had
non-const pointers, there would be no argument. Const variables
have to be initalised, and they cannot change. In that situation
pointers to single objects would behave like references. However,
(a) we are not in that situation, the language allows to have non-
const pointers, and (b) pointers are also used to iterate over
arrays (you really can't do that with references, I'd like to see
you try, though).

Victor



Matthias Reik 06-27-2003 10:14 PM

Re: Pointers: What's the point?
 
Victor Bazarov wrote:
> "Matthias Reik" <matthias.reik@cenara.com> wrote...
>
>>Some comments:
>>
>>
>>
>>>>Wrong. There are a number of differences:
>>>>
>>>>* pointers can be NULL, or not initialized
>>>

>>What's the point of not initializing a pointer?

>
>
> The point is to save time. If you know that the pointer is
> going to be calculated somehow, why initialise it to anything
> before that?

Saving time is a good idea, but often you are just building your own
traps. It often works fine as long as you are following the standard
execution path, but (very often) as soon as something unexpected
happens, your assumtpions will be wrong. Yes, in some cases you
don't need to do this, but would you consider this being the rule?
I wouldn't

>
>>How do you know your
>>pointer is pointing to a real object or into nirvana?

>
>
> You know if you made it point somewhere.

So you need to keep track whether it's pointing somewhere? Wouldn't
that be easier if you would init it to NULL

>
>
>>>>* temporaries can be bound to const references only.
>>>>* Pointers are objects and can be memcpy'ed
>>>>* Copy ctors must use references
>>>

>>Who said that ctors MUST use references?

>
>
> The Standard said that.

Are you REALLY sure, I don't think so (even so I haven't got the
standard in reachable distance).

>
>>You can equally well use a
>>pointer for that... yeah, you should be a litte bit more careful, to not
>>confuse people, but that't another story :)

>
>
> That wouldn't be a copy constructor. It would be just
> another parametrised one.
>
>>>>* Pointer values can be changed, references stay bound to
>>>> the same object during their lifetime.
>>>

>>Well this makes the assumption, that you are are talking about non-const
>>pointers

>
>
> Feeling like nit-picking today, aren't we?

no, but talking about something that is seldomly used. What are you
allowed to changed if you have got:
fn(Object const* const)?

>
>
>>>>* Pointers support pointer arithmetic.
>>>>
>>>>That's not quite "exactly the same".
>>>>
>>>>
>>>>
>>>>>That said, we can rephrase
>>>>>the question, "What's the point of pointers and references?" to just
>>>>>"What's the point of references?" (or, "What's the point of
>>>>

> pointers?").
>
>>>>You can't. Some things require pointers, some things require references.
>>>

>>What is it that would require a reference? I would say it more the
>>following way: You can do everything with pointers, but some things are
>>easier and more clear with references.

>
>
> One thing you can do with references that you cannot do with
> pointers: rest assured that in a legal program there is always
> an object behind a reference. Once you have a reference, you
> have an object. Not so with pointers.
>
> Victor
>
>




Norbert Riedlin 07-01-2003 08:30 PM

Re: Pointers: What's the point?
 

"Matthias Reik" <matthias.reik@cenara.com> schrieb im Newsbeitrag
news:3EFB74D8.5010209@cenara.com...
....
> >>You can't. Some things require pointers, some things require references.

> What is it that would require a reference? I would say it more the
> following way: You can do everything with pointers, but some things are
> easier and more clear with references.
>

challenge:
given the following codesnippet:

#include <iostream>

struct A {
A() : a(42) {}
int a;
};

// some operator<< ()

int main() {
A a;

std::cout << "Hello world. And the answer is: " << a << std::endl;
}

Could you declare and an operator<<(), _without using references_, so that:
1. the code compiles
2. it creates reasonable output.

Much fun

Norbert






All times are GMT. The time now is 04:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.