q for the C hackers

Kevin D. Clark kclark at CetaceanNetworks.com
Tue Aug 19 12:14:19 EDT 2003


Tom Fogal <tfogal at io.iol.unh.edu> writes:

> The const is purely optional; you could just as easily remember yourself that
> 'hey, i dont want to change that value in this function' and simply not do it.
> The justification i was given for such usage is that someone who is not you
> can quickly look at the function header/prototype and see, 'oh, this value is
> const, it wont change when i pass it to the function'.
> Also, supposedly it prevents you from 'forgetting' you didnt mean to change it,
> and doing so on accident. IMHO though, if you can't remember you didn't want
> to change a fqn parameter, you're functions are waaaay to long, or you aren't
> cut out for the programming vocation :P

(Hmm.  Maybe I'm not cut out for programming...)


I must politely, but strenuously disagree.  "const" is incredibly
useful.

I can think of this large class library that I had trouble using in
the past, all because the people who implemented it had a bizarre idea
of who owned what memory.  Instead of being able to code to a
well-defined interface (and this involves "const"), I was told to look
at the implementation of each function/method.  This was tedious,
inconsistant, and when the underlying implementation changed (and it
did....often...), I found myself debugging code into the wee hours.

In a large project, "const" is invaluable.

[snip]

> Oh yes, this is a source of much confusion. You see, C++ introduced this idea
> called a 'reference', which, for all intents and purposes is a pointer, but
> you dont have to dereference it as such. thus if i have a procedure that wants
> to change its integer parameter to 5, i could do the following in C++:
> 
> void change_int(int &changeme) { changeme = 5; }
> 
> C programmers would flip at such a statement - 'Youll screw everything up! That
> makes changeme have whatever is in memory location "5"! That could be 
> ANYTHING!' etc etc.
> Well, thats true. In C, &changeme means 'the address of changeme'. In C++, the
> &changeme could mean EITHER 'the address of changeme' OR 'a reference to 
> changeme'. I've never had a good explanation as to how one could tell which
> is meant. It seems to me that if its passed as a function parameter, it is a
> reference. Within a function, it means 'address of'. Don't quote me on that.
> 
> Another CS major concurs with my interpretation, acutally.. perhaps I got it
> right...

I find this to be a bit confused...

1:  In C++, if "a" is a reference to "b", then "a" is ANOTHER NAME FOR
    "b".  THAT'S IT.  No magic here.

2:  C++ references are different from Java references in that it is
    not possible/legal to have a null reference.  A lot of people get
    hung up on this...

3:  C++ references might be implemented in terms of pointers, but you
    shouldn't care about this.


There you go.

--kevin
-- 
Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA)
cetaceannetworks.com!kclark (GnuPG ID: B280F24E)
alumni.unh.edu!kdc




More information about the gnhlug-discuss mailing list