On 2007-12-03 13:23, Michael Bell wrote:
In message <cd************ *************** *******@s8g2000 prg.googlegrou
ps.com>
Salt_Peter <pj*****@yahoo. comwrote:
>___
All this to state:
a) Use a reference instead of a pointer whenever you can. Its the best
insecticide i know.
b) If you must use a pointer, always initialize it (if you can't give
it a valid address yet - then null it)
This is jargon I don't know. Does "reseat" mean to point a pointer at
some other address? That seems to the meaning of
Reseat means to make it refer to something else. A reference can only
refer to one thing, and you have to decide what that is when you create
it, with a pointer on the other hand you can change what it points to.
>int m(77);
p = &m; // reseated pointer
What exactly is going on here?
m is created and given the value 77, then the address of m is taken and
is stored in p. This means that p will now point to m.
>int j;
j has been declared. No problems about that!
>int& ref = j; // ref is permanently bound
What has happened here?
When you create the reference ref you say that it refers to j. After
this you can think of ref as an alias for j, ref and j are now two
different names for the same thing.
>// ref = m; // error
ref = 55; // ok, j is now set to 55
Sorry, I just don't understand.
Since ref and j are the same thing, setting the value of ref is the same
as setting the value of j.
How about this analogy: there is this guy named Bob, when someone talks
about Bob they mean him. However Bob has a son, and when he talks about
Bob he call him "dad". In this case dad is a reference to Bob, they both
refer to the same thing.
Bob is the boss at a company, and when his employees talk about the boss
they talk about him, but if Bob should be sacked and a new boss hired
the employees would still talk about the boss, but no longer about Bob.
In this case "the boss" is a pointer, it refers to a person but
indirectly and who it refers to can thus be changed.
--
Erik Wikström