 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gavin Deane Guest
|
Posted: Mon Jun 23, 2003 10:29 pm Post subject: Re: Pointers: What's the point? |
|
|
Alexander Terekhov <terekhov (AT) web (DOT) de> wrote
| Quote: | P.S. Tim, don't take Bazarov seriously. He's just one of many
seriously addicted c.l.c++'s "whimper-plonker" regular trolls.
|
Tim, don't let others make up your mind for you. Have a look through
the history of the group. Read posts from Alexander Terekhov and
Victor Bazarov. Form your own opinions.
|
|
| Back to top |
|
 |
Adie Guest
|
Posted: Mon Jun 23, 2003 11:03 pm Post subject: Re: Pointers: What's the point? |
|
|
Gavin Deane wrote:
| Quote: | Alexander Terekhov <terekhov (AT) web (DOT) de> wrote
P.S. Tim, don't take Bazarov seriously. He's just one of many
seriously addicted c.l.c++'s "whimper-plonker" regular trolls.
Tim, don't let others make up your mind for you. Have a look through
the history of the group. Read posts from Alexander Terekhov and
Victor Bazarov. Form your own opinions.
|
Exactly, Bazarov is a reknown newsgroup wrecker/pirate/troll.
|
|
| Back to top |
|
 |
Gavin Deane Guest
|
Posted: Tue Jun 24, 2003 2:20 pm Post subject: Re: Pointers: What's the point? |
|
|
Adie <a_usenetizen (AT) hotmail (DOT) com> wrote
| Quote: | Gavin Deane wrote:
Alexander Terekhov <terekhov (AT) web (DOT) de> wrote
P.S. Tim, don't take Bazarov seriously. He's just one of many
seriously addicted c.l.c++'s "whimper-plonker" regular trolls.
Tim, don't let others make up your mind for you. Have a look through
the history of the group. Read posts from Alexander Terekhov and
Victor Bazarov. Form your own opinions.
Exactly, Bazarov is a reknown newsgroup wrecker/pirate/troll.
|
Tim, don't let others make up your mind for you. Have a look through
the history of the group. Read posts from Adie and Victor Bazarov.
Form your own opinions.
GJD
|
|
| Back to top |
|
 |
Gavin Deane Guest
|
Posted: Tue Jun 24, 2003 4:49 pm Post subject: Re: Pointers: What's the point? |
|
|
Alexander Terekhov <terekhov (AT) web (DOT) de> wrote
| Quote: | Gavin Deane wrote: ...
Tim, there's a whole bunch of trolls here, BTW. Wahler, Default,
to name a few. Well, The Great Master is, of course, NeilB. Now,
Gavin, what can you say?
|
Tim, don't let others make up your mind for you. Have a look through
the history of the group. Read posts from ...
No. I'm bored now.
|
|
| Back to top |
|
 |
foo Guest
|
Posted: Tue Jun 24, 2003 8:07 pm Post subject: Re: Pointers: What's the point? |
|
|
[email]Michiel.Salters (AT) cmg (DOT) nl[/email] (Michiel Salters) wrote in message news:<cefd6cde.0306230254.63b0385c (AT) posting (DOT) google.com>...
| Quote: | "no_sp@m" <no_sp@m> wrote
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.
|
|
| Back to top |
|
 |
Matthias Reik Guest
|
Posted: Thu Jun 26, 2003 10:34 pm Post subject: Re: Pointers: What's the point? |
|
|
Some comments:
| Quote: | 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?
| Quote: | * 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 :)
| Quote: | * 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
| Quote: | * 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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jun 27, 2003 9:42 pm Post subject: Re: Pointers: What's the point? |
|
|
"Matthias Reik" <matthias.reik (AT) cenara (DOT) com> wrote...
| Quote: | Victor Bazarov wrote:
"Matthias Reik" <matthias.reik (AT) cenara (DOT) 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.
| Quote: | 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.
| Quote: | * 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.
| Quote: | 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
|
|
| Back to top |
|
 |
Matthias Reik Guest
|
Posted: Fri Jun 27, 2003 10:14 pm Post subject: Re: Pointers: What's the point? |
|
|
Victor Bazarov wrote:
| Quote: | "Matthias Reik" <matthias.reik (AT) cenara (DOT) 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
| Quote: |
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
| Quote: |
* 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).
| Quote: |
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)?
| Quote: |
* 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
|
|
|
| Back to top |
|
 |
Norbert Riedlin Guest
|
Posted: Tue Jul 01, 2003 8:30 pm Post subject: Re: Pointers: What's the point? |
|
|
"Matthias Reik" <matthias.reik (AT) cenara (DOT) com> schrieb im Newsbeitrag
news:3EFB74D8.5010209 (AT) cenara (DOT) com...
....
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|