Re: I'm not REALLY a newbie, but.......
From: Leor Zolman (leor_at_bdsoft.com)
Date: 06/02/04
- Next message: Francis Glassborow: "Re: I'm not REALLY a newbie, but......."
- Previous message: Francis Glassborow: "Re: Concept of union"
- In reply to: as mellow as a horse: "Re: I'm not REALLY a newbie, but......."
- Next in thread: Francis Glassborow: "Re: I'm not REALLY a newbie, but......."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 01 Jun 2004 19:28:00 -0400
On Tue, 1 Jun 2004 23:45:42 +0100, "as mellow as a horse"
<mail@MICKmoss42.fslife.co.uk> wrote:
>Yes, this works! Emergency over! Thanks!
>
>Problem is I'm *very* rusty like I said, and can't quite work out for sure
>*why* it works.
If you choose to use only pointers and not references, then the /only/ way
a function can legitimately change a caller's local variable is if the
called function has access to the variable's /address/. Thus, for any type
T, a caller needs to provide to that function an expression of type
"pointer to T" in order for the function to change the T variable. If you
begin with T being 'int', it is easy enough to see that the function must
be given an argument of type 'int *' if it is going to have a prayer of
altering the value of that int. Now just add a level of indirection to the
original variable: instead of it being an 'int' you want the function to
change, have it be an 'int *'. Well, if the variable is an 'int *' and the
function has to have its address, the type of that argument is going to
be... 'int **'. That's a /pointer/ to a 'pointer to int' (if that helps
any...)
> I'm sure I must have written a hundred functions in the
>distant past that swapped dynamic objects around (sorry, pointers to them!).
>But I don't recall ever using pointers to pointers except when messing
>around with n-dimensional arrays, or arrays of strings (or should I say
>arrays of character arrays, I never did bother with the standard library and
>all that as I was doing C for five years on and off before I ever tried
>C++). I thought passing the pointer was enough rather than passing the
>address of the pointer as this code appears to be doing.
It would be if you'd let us tell you about using references ;-)
> I suppose that if
>that *was* enough,
Stop right there...if the function has to change the value of an object,
getting the current /value/ of the object will never be "enough" (unless
perhaps the value of an object happens to be its own address, or something
contrived like that.)
> I'd have to dereference the pointer every time I wanted
>to refer to the actual object in the called function.
Well, generally speaking, that's life with pointers. When you pass a
pointer, then the function you're calling has to dereference it to get to
the underlying object. One appeal of references (they do seem to keep
coming up, don't they?) is that the "dereferencing", if you choose to see
it that way (I used to, but now after having been set straight by several
folks in this group, no longer really look at it that way except when
thinking about typical implementations), is implicit rather than explicit.
That makes for code much neater in appearance.
> I don't even know if
>that makes sense as it's getting late where I am and my brain's turning to
>sludge (no change there then).
Things will look much clearer in the morning, I'd wager.
BTW, note that you can now bite the bullet and use std::swap with no fear
of incurring unnecessary overhead. The line:
std::swap(account1, account2);
will swap your pointers just fine without the need for /anybody's/ custom
function ;-)
Good luck,
-leor
-- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Next message: Francis Glassborow: "Re: I'm not REALLY a newbie, but......."
- Previous message: Francis Glassborow: "Re: Concept of union"
- In reply to: as mellow as a horse: "Re: I'm not REALLY a newbie, but......."
- Next in thread: Francis Glassborow: "Re: I'm not REALLY a newbie, but......."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|